MickeyOoh

MickeyOoh

"defstruct" diffrenet behaviors in iex and elixir

Programming Elixir page 80 about “defstruct”

I first ask question and I am a quite beginner. I followed this book again to confirm each steps’ process.

Following book by using iex, it works. But implemented IO.puts into code and did “Elixir defstruct.exs” so that it didn’t work with Error.
please help me why the error occur and how to solve this.
code:

defmodule Subscriber do
  defstruct name: "", paid: false, over_18: true
end
s1 = %Subscriber{}
IO.puts inspect(s1)

Error:
** (CompileError) defstruct.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

Marked As Solved

peerreynders

peerreynders

The issue is with the phases that the Elixir compiler goes through.

While this

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

end
s1 = %Subscriber{}
IO.puts inspect(s1)

results in an error, the following does not

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

  def new,
    do:  %Subscriber{}

end
s1 = Subscriber.new()
IO.puts inspect(s1) 

During compilation the expansion phase happens before the evaluation phase. However the functionality of a module isn’t “defined” and available until it has been evaluated during the evaluation phase. So when the compiler comes across

s1 = %Subscriber{}

during the expansion phase it tries to expand %Subscriber{} - the problem is that %Subscriber{} is part of the Subscriber module which won’t be defined until later in the evaluation phase - so during the expansion phase there is no definition for %Subscriber{} (yet).

s1 = Subscriber.new()

doesn’t have anything to expand - it is simply a function call that is evaluated during the evaluation phase - by that point in time the Subscriber module has been fully evaluated and both %Subscriber{} and Subscriber.new() are defined and available.

See also

Main phases of Elixir compilation

Also Liked

NobbZ

NobbZ

I do not think so!

Defining a struct and using it in the following script is essential in scripting with elixir.

MickeyOoh

MickeyOoh

peerreynders, Thank you so much.
I corrected my codes as what you said and it works. And I learned a lot from you.

modified code:

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

  def run do
    s1 = %Subscriber{}
    IO.puts inspect(s1)
    s2 = %Subscriber{name: "Dave"}
    IO.puts inspect(s2)
    s3 = %Subscriber{ name: "Mary", paid: true}
    IO.puts inspect(s3)
    IO.puts "#{s3.name}"
    %Subscriber{name: a_name} = s3
    IO.puts a_name
    s4 = %Subscriber{ s3 | name: "Marie"}
    IO.puts inspect(s4)
  end 
end

Subscriber.run()

Where Next?

Popular in Questions Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

We're in Beta

About us Mission Statement