#elixirnewbie
1. The shorthand sigil syntax is great for structs.
defmodule User do
destruct ~w[name email]a
end
The ~w is a sigil with an array of words; the trailing a makes these atoms; the [] instead of () is a valid use of delimiters.
u = %User{}
-> %User{email: nil, name: nil}
Map.keys u
-> [:__struct__, :email, :name]
u.__struct__
-> User
u[:name]
-> ...User does not implement the Access behaviour
u |> Enum.map(f(x) -> x end)
-> ...protocol Enumerable not implemented for %User...
def new(fields)
fields
|> User.__struct__
|> validate
|> ...
end
...
User.new(name: "BT", email: "struct@bucket.com")
...
@enforce_keys [:name]
destruct [:name, :email]
...