Requiring Keys For Structs
When defining a struct
in Elixir, we may want to ensure that certain values are provided. We can require that certain keys are used in the creation of a struct with the @enforce_keys
attribute. Here is an example of a Name
struct.
With this defined, we can create a struct that uses all of the keys.
We can also ignore :middle
and just provide the required keys.
We cannot, however, omit any of the keys specified in @enforce_keys
. If we do omit any of them, Elixir will raise an error.
Last updated