I have code which tries to use a struct defined in a nested module. I get a compile error if I use the struct before the submodule, but not if I use the struct after the submodule.
It feels like a namespacing/aliasing issue, given how submodule definitions are supposed to implicitly set up an alias. But then why doesn’t the maual alias at the top of the file resolve the issue?
Code reproduced below, also available at https://repl.it/repls/JuicyUnnaturalAdministration#main.exs
defmodule Outer do
alias Outer.Inner # makes no difference if this exists or not
# With `main/0` here, we get the error
# "Outer.Inner.__struct__/1 is undefined"
def main() do
IO.inspect(%Inner{})
end
defmodule Inner do
defstruct [foo: "hi"]
end
# Move the `main` definition here and it compiles.
end