defmodule Subscriber do
defstruct name: "", paid: false, over_18: true
end
subscriber = %Subscriber{}
** (CompileError) maps/subscriber.exs:5: cannot access struct Subscriber, the struct was not yet defined or the struct is being accessed in the same context that defines it
(elixir) src/elixir_map.erl:44: :elixir_map.translate_struct/4
--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You cannot use the a struct in the same context it is defined. If you split the definition and usage into separate files it will work.
Not sure I understand the question. What you name the module has nothing to do with where you can use the struct.
To clarify. Where you can use the struct depends on when things are expanded by the compiler and when they are evaluated. Take this example:
defmodule Foo do
defstruct []
end
%Foo{}
The above code won’t work because the compiler will try to expand %Foo{}
before the module or the struct is defined. Expansion happens before evaluation and the struct will be defined when the call to defmodule
is evaluated.
In your example you can use struct inside the function because macros in the function are expanded after the struct is defined.
--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.