CompileError while creating a struct

553 views
Skip to first unread message

Dzianis Dashkevich

unread,
Nov 15, 2014, 11:31:28 AM11/15/14
to elixir-l...@googlegroups.com
Hi everyone,

I'm trying to define a simple struct and create one in a single file:

defmodule Subscriber do
  defstruct
name: "", paid: false, over_18: true
end

subscriber
= %Subscriber{}

When I finally execute it with elixir subscriber.exs it fails:

** (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

But it succeeds when I execute it without the last line in it. Am I missing something?

Eric Meadows-Jönsson

unread,
Nov 15, 2014, 12:13:19 PM11/15/14
to elixir-l...@googlegroups.com
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.

--
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.



--
Eric Meadows-Jönsson

Dzianis Dashkevich

unread,
Nov 15, 2014, 12:29:35 PM11/15/14
to elixir-l...@googlegroups.com
Thanks, Erik!

Onorio Catenacci

unread,
Nov 19, 2014, 11:00:05 AM11/19/14
to elixir-l...@googlegroups.com
On Saturday, November 15, 2014 12:13:19 PM UTC-5, Eric Meadows-Jönsson wrote:
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.


Hi Eric,

I saw your response and I wanted to ask for a little further clarification.  I've got code analogous to this:

defmodule Test do

  defmodule Test.Struct do
    defstruct fields: [] , field_types: [], results: [] 
    @type t :: %Test.Struct{fields: List, field_types: List, results: List}
  end

  def testf do
    ts = %Test.Struct{
      :fields => [],
      :results => []
    }
  end
end    

And I can call testf just fine.  Does naming the module in which the struct is defined as Test.Struct qualify as a different context?  I'm assuming it does but I just wanted to insure I've got the right understanding of the situation.

--
Onorio
 

Eric Meadows-Jönsson

unread,
Nov 19, 2014, 1:24:29 PM11/19/14
to elixir-l...@googlegroups.com

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.



--
Eric Meadows-Jönsson

Onorio Catenacci

unread,
Nov 20, 2014, 9:22:28 AM11/20/14
to elixir-l...@googlegroups.com
Thanks for the clarification Eric!

--
Onorio
Reply all
Reply to author
Forward
0 new messages