Elixir __struct__/0
When we define a struct via defstruct
macro we end up getting a __struct__/0
function on both struct module definition and on each struct map. The intriguing part is that the implementation of the module and the map are different, check this out:
iex(1)> defmodule Book do
...(1)> defstruct title: nil, pages_count: 0
...(1)> end
iex(2)> Book.__struct__()
%Book{pages_count: 0, title: nil}
iex(3)> Book.__struct__().__struct__()
Book
As we can see Book.__struct__()
returns a new %Book{}
struct with its defaults, meanwhile %Book{}.__struct()
returns the Book
module.