holasoymas

holasoymas

Customer.__struct__/1 is undefined, cannot expand Customer

nested_dict.exs

defmodule Customer do
  defstruct name: "", company: ""
end

defmodule BugReport do
  defstruct owner: %Customer{}, details: "", severity: 1
end

I got this error after writing this in iex
iex(1)> c "nested_dict.exs"

error: Customer.__struct__/1 is undefined, cannot expand struct Customer. Make sure the struct name is correct. If the struct name exists and is 
correct but it still cannot be found, you likely have cyclic module usage in your code
    │
  4 │   owner: %Customer{name: "Dave", company: "PragProg"},
    │          ^
    │
    └─ nested_dict.exs:4:10

I also try running it using elixir nested_dict.exs got the same error.


I again also run them in through different file:

Customer.exs

defmodule Customer do
  defstruct name: "", company: ""
end

nested_dict_api.exs

Code.require_file("Customer.exs")

defmodule BugReport do
  defstruct owner: %Customer{}, details: ""
end

nested_dict.exs

Code.require_file("nested_dict_api.exs")

report = %BugReport{
  owner: %Customer{name: "Dave", company: "PragProg"},
  details: "broken"
}

IO.inspect(report)

Again, the same error ,

I am pretty sure that there are no typo in my code as fas as i know

Can someone please tell me what’s wrong in my code and how to solve it ?

Thank you,

Elixir : 1.17.0 (compiled with Erlang/OTP 27)
OS : Linux Mint 21

First Post!

al2o3cr

al2o3cr

Can you post the exact contents of nested_dict.exs? Line 4 doesn’t match what’s in your post, and the ordering of Customer and BugReport in the file is important.

Most Liked

Eiji

Eiji

That’s a scope problem in iex and single file scripts. Since everything is compiled at once (not each file into .beam file inside _build directory) the code must work as custom modules are not defined at compile-time. Using them run-time is definitely not a problem. This behaviour is not best, but very reasonable.

This should do the job:

defmodule Customer do
  defstruct name: "", company: ""
end

defmodule BugReport do
  defstruct owner: nil, details: "", severity: 1

  def new(data \\ %{}, customer \\ nil) do
    owner = case customer do
      struct when is_struct(struct, Customer) -> struct
      map_or_list when is_map(map_or_list) or is_list(map_or_list) -> struct(Customer, map_or_list)
      _customer -> %Customer{}
    end

    map = Map.new(data)
    struct(%__MODULE__{owner: owner}, map)
  end
end

This allows us to write code like:

iex> BugReport.new()
%BugReport{owner: %Customer{name: "", company: ""}, details: "", severity: 1}

iex> BugReport.new(details: "…")
%BugReport{owner: %Customer{name: "", company: ""}, details: "…", severity: 1}

iex> BugReport.new([], [name: "xyz"])
%BugReport{owner: %Customer{name: "xyz", company: ""}, details: "", severity: 1}

In both cases you are able to use Keyword lists as well as Map and in customer case also Customer struct (of course as long as call with %Customer{…} argument is in run-time).

Last Post!

holasoymas

holasoymas

defmodule Customer do
  defstruct name: "", company: ""
end

defmodule BugReport do
  defstruct owner: %Customer{}, details: ""
end

report = %BugReport{
  owner: %Customer{name: "Dave", company: "PragProg"},  # this is line 10, error line  
  details: "broken"
}
  error: cannot access struct Customer, the struct was not yet defined or the struct is being accessed in the same context that defines it
    │
 10 │   owner: %Customer{name: "Dave", company: "PragProg"},
    │          ^
    │
    └─ nested_dict_api.exs:10:10

** (CompileError) nested_dict_api.exs: cannot compile file (errors have been logged)

NOTE, this is when i put all my modules in one file.


when i put all modules in different file
#Customer.exs
defmodule Customer do
  defstruct name: "", company: ""
end
#nested_dict_api.exs
Code.require_file("Customer.exs")

defmodule BugReport do
  defstruct owner: %Customer{}, details: ""
end
#nested_dict.exs
Code.require_file("nested_dict_api.exs")
Code.require_file("Customer.exs")

report = %BugReport{
  owner: %Customer{name: "Dave", company: "PragProg"},   #error is here on line, line 5 
  details: "broken"
}

IO.inspect(report)

error

    error: Customer.__struct__/1 is undefined, cannot expand struct Customer. Make sure the struct name is correct. If the struct name exists and is 
correct but it still cannot be found, you likely have cyclic module usage in your code
    │
  5 │   owner: %Customer{name: "Dave", company: "PragProg"},
    │          ^
    │
    └─ nested_dict.exs:5:10

** (CompileError) nested_dict.exs: cannot compile file (errors have been logged)

Both method WORK ON iex but not on elixir <filename>.exs command

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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
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
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement