Kernel.struct
You're seeing just the function
struct
, go back to Kernel module for more information.
Specs
Creates and updates a struct.
The struct
argument may be an atom (which defines defstruct
)
or a struct
itself. The second argument is any Enumerable
that
emits two-element tuples (key-value pairs) during enumeration.
Keys in the Enumerable
that don't exist in the struct are automatically
discarded. Note that keys must be atoms, as only atoms are allowed when
defining a struct. If keys in the Enumerable
are duplicated, the last
entry will be taken (same behaviour as Map.new/1
).
This function is useful for dynamically creating and updating structs, as
well as for converting maps to structs; in the latter case, just inserting
the appropriate :__struct__
field into the map may not be enough and
struct/2
should be used instead.
Examples
defmodule User do
defstruct name: "john"
end
struct(User)
#=> %User{name: "john"}
opts = [name: "meg"]
user = struct(User, opts)
#=> %User{name: "meg"}
struct(user, unknown: "value")
#=> %User{name: "meg"}
struct(User, %{name: "meg"})
#=> %User{name: "meg"}
# String keys are ignored
struct(User, %{"name" => "meg"})
#=> %User{name: "john"}