Charlie
Charlie

Reputation: 4329

Elixir - What does the 'use' keyword do?

I suspect it's an elementary question, but it's been hard to find a succinct, canonical answer online.

From what little I understand;

Can anyone clarify?

Upvotes: 18

Views: 4862

Answers (1)

Paweł Obrok
Paweł Obrok

Reputation: 23164

It requires the given module and then calls the __using__/1 callback on it allowing the module to inject some code into the current context. See https://elixir-lang.org/getting-started/alias-require-and-import.html#use.

Example:

defmodule Test do
  use Utility, argument: :value
end

is about the same as

defmodule Test do
  require Utility
  Utility.__using__(argument: :value)
end

Upvotes: 32

Related Questions