Double underscore features in Elixir

There are several double underscore functions, such as __before_compile__ , that automatically call compile time in Elixir. However, I also see several __functions__ double underscores that seem to be called without magic requirements. For example, in Ecto, the following 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 response. The method is the term oop and is inappropriate here.

+7
elixir
source share
1 answer

Double counting 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 underscores are not automatically imported (this is what we want here).

PS: these are functions, not methods. :)

+13
source share

All Articles