shankardevy
shankardevy

Reputation: 4370

Double underscore functions in 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.

Upvotes: 10

Views: 859

Answers (1)

José Valim
José Valim

Reputation: 51359

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

Upvotes: 20

Related Questions