Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double underscore functions in Elixir

Tags:

elixir

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.

like image 939
shankardevy Avatar asked May 19 '15 18:05

shankardevy


1 Answers

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. :)

like image 68
José Valim Avatar answered Oct 29 '22 20:10

José Valim