3 ms·
Correct, `use` is a part of Elixir. When you `use` a module like `use FooModule`, it runs the `__using__` macro defined in `FooModule`. Because `__using__` is a
by asa400 2y ago
Correct, `use` is a part of Elixir. When you `use` a module like `use FooModule`, it runs the `__using__` macro defined in `FooModule`. Because `__using__` is a macro, it serves as an extension point that allows modules that define it to "inject" code into the modules that `use` it. This injected code could be anything: function definitions, constants, module definitions, etc.
For example:
defmodule A do
defmacro __using__(_env) do
quote do
defmodule Math do
def plus_itself(y) do
y + y
end
end
end
end
end
defmodule B do
use A
end
B.Math.plus_itself(10) |> IO.inspect() #=> 20
More info:
https://hexdocs.pm/elixir/alias-require-and-import.html#use https://hexdocs.pm/elixir/alias-require-and-import.html#use