I’m actually quite undecided on that part. The OOP-developer in me really wants a new
-function, as it allows me to have the required fields as required parameters, and then an optional keyword-list or map as the last parameter to fill in optional fields. (It also allows fields to be calculated).
However, lately I am questioning that approach for several reasons: First, the Elixir compiler is quite good at complaining about missing required fields. Second, that last optional parameter taking a map or keyword-list is really not nice to deal. Third, when you have more than three required fields, the approach results in a large number of parameters to the new
-function.
Now I am trying to embrace structs as open (non-hiding) data structures. No constructors, and no calculated fields.
“Nested” in the sense of namespace, not in code, though. I really try to keep strictly to one-module per file. I would have the following directory structure:
lib/
order_info/
user.ex
order_info.ex
order_info.ex
would then contain something like this:
defmodule OrderInfo do
use TypedStruct
typedstruct do
..
end
and, order_info/user.ex
:
defmodule OrderInfo.User do
use TypedStruct
typedstruct do
..
end