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

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
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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
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
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
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
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
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement