There are some double underscore functions like __before_compile__
which are automatically called on compile time in Elixir. However, I also see a number of double underscore __functions__
that seem to be named without magical requirement. For eg, in Ecto, the below functions are called
Ecto.Schema.__source__(source),
Ecto.Schema.__fields__(fields),
Ecto.Schema.__assocs__(assocs),
Ecto.Schema.__primary_key__(primary_key_field),
What qualifies these __functions__
to have, well, double underscores?
ps: renamed 'methods' to 'functions' after jose's answer. Method is an oop term and is inappropriate here.
The double score functions are used as callbacks (__before_compile__
, etc) or for metadata (__info__
, etc). The goal is that they should not pollute your module API. Also functions starting with underscore are not automatically imported (which is what we want here).
PS: they are functions, not methods. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With