ExConstructor.__using__
__using__
, go back to ExConstructor module for more information.
use ExConstructor
defines a constructor for the current module's
struct.
If name_or_opts
is an atom, it will be used as the constructor name.
If name_or_opts
is a keyword list, name_or_opts[:name]
will be
used as the constructor name.
By default, :new
is used.
Additional options in name_or_opts
are stored in the
@exconstructor_default_options
module attribute.
The constructor is implemented in terms of populate_struct/3
.
It accepts a map or keyword list of keys and values map_or_kwlist
,
and an optional opts
keyword list.
Keys of map_or_kwlist
may be strings or atoms, in camelCase or
under_score format.
opts
may contain keys strings
, atoms
, camelcase
and underscore
.
Set these keys false to prevent testing of that key format in
map_or_kwlist
. All default to true
.
For the default name :new
, the constructor's definition looks like:
@spec new(ExConstructor.map_or_kwlist, Keyword.t) :: %__MODULE__{}
def new(map_or_kwlist, opts \\ []) do
ExConstructor.populate_struct(%__MODULE__{}, map_or_kwlist, Keyword.merge(@exconstructor_default_options, opts))
end
defoverridable [new: 1, new: 2]
Overriding new/2
is allowed; the generated function can be called by
super
. Example uses include implementing your own opts
handling.