Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why error -- undefined function defstruct

Tags:

elixir

I have this modules where I'm trying to define a struct:

defmodule A do
  defmodule B do
    defstruct :id, :name
  end
end

Why error?

undefined function defstruct/2

Why is this error?

like image 363
Torito Avatar asked May 30 '26 05:05

Torito


1 Answers

Elixir interprets defstruct :id, :name as calling defstruct with 2 arguments, that's the /2 part in undefined function defstruct/2.

What you want to do is pass a single argument to defstruct, a list of field names:

defmodule A do
  defmodule B do
    defstruct [:id, :name]
  end
end
like image 184
nietaki Avatar answered Jun 02 '26 19:06

nietaki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!