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

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

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
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
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
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
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
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

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
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
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
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
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

We're in Beta

About us Mission Statement