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

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
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
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
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
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
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

We're in Beta

About us Mission Statement