The ‘use’ Macro in Elixir.

Information on ‘use’ is Hard to Find.

  • ‘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’?

  • 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?

Use Allows You to Create Templates.

How Phoenix Leverages ‘use’.

Summary

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

My experience self hosting a Nextcloud Installation for my Family

Android Gallery scrolling behind the scene!

RT-Thread Announces Partnership with Open Source LVGL

AWS CodeArtifact with Maven — Further adventures with ServerLess

oval stone with the word “Tranquility” and a Chinese character engraved on it.

How Grab is Blazing Through the Super App Bazel Migration

Golang Frameworks and External Libraries Performace

Queue in Java..!

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.

More from Medium

Deploy Phoenix 1.6 / Elixir 1.3 on Render.com: A Working Guide

A Strange pitfall with Map.get & its default behavior | Elixir Basics

Hot Code Reloading of Elixir OTP Application

Elixir 1.13 released: The developer’s point of view