But at least for struct/2 any enumerable is allowed according to the typespecs. So struct Page, %{title: "koko"} is as valid as struct Page, title: "koko" is.
In order to create a struct you have to pass it a keyword list:
defmodule User do
defstruct [name: "", age: nil]
end
Why do we do that and why don’t we pass in maps, since Structs are really maps.
For example is_map(%User{}) would return true. But then Structs accept keyword
lists instead of maps, which is kind of weird I think.
You defining it (at compile time), not creating it (at runtime).
defmodule User do
defstruct [name: "", age: nil] # defining struct at compile time
def new(name, age \\ nil) do
%User{age: age, name: name} # creating struct at run time
end
end