Referencing other module in Elixir

There are several different ways of referencing a function in another module.

The direct:

MyModule.SubModule.hello

You can also alias the module so that a shorter name can be used:

alias MyModule.SubModule
Submodule.hello

You can import a module so that all the functions are treated as local to this module:

import MyModule.SubModule
hello

You can require a module so that you can use the macros defined in that module.

You can use a module to run the __using__ function from that module in your context.

These don’t have to be at the top of the module, you can use them within a function to restrict scope (not so sure about using __using__ this way…).

These also have parameterized versions so that you can choose what to import (only some, exclude some, rename them).

This can make them seem complex …

Leave a comment