The ‘use’ Macro in Elixir.

Information on ‘use’ is Hard to Find.

I found learning ‘use’ unintuitive. Partially because I think it’s hard to find documentation for.

  • ‘use’ is not searchable: Google searching “how to use ‘use’ in elixir” doesn’t return much information.
  • ‘use’ is hidden in the elixir documentation: It’s in the Elixir documentation for alias, require, and import.
  • There is no example for __using__ in a module: The elixir documentation states that ‘use’ calls the __using__ callback, but there’s no example implementation of this callback.

So What Is ‘use’?

here’s a lightweight explanation of ‘use’ in elixir as I currently understand it.

  • Use is a tool to reuse code just like require, import, and alias.
  • Use simply calls the __using__ macro defined in another module.
  • The __using__ macro allows you to inject code into another module.

Why ‘use’ Instead of Import?

Looking at the above example, you might wonder why you should use ‘use’ instead of import. Import allows you to import all of the functions from a module, so you could accomplish the same goal like so:

Use Allows You to Create Templates.

Have a common pattern? use allows you to codify the pattern into a template for making other modules.

How Phoenix Leverages ‘use’.

Phoenix is a powerful example of how to leverage ‘use.’


Anytime you have a common pattern you want to repeat for modules, you can consider ‘use.’ For other cases, you should consider sticking with alias, require, and import. However, be cautious not to overuse the ‘use’ macro because it can hide functionality and behavior.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Brooklin Myers

Brooklin Myers

Software Engineer. I create educational content focused on technology for mobile and web applications.