How does __using__ actually work?

I will be short:

defmodule A do
    use B

    def func1 do
        callback = &func2/1
        # then I need to send callback to another function call

    # that's all, there is no definition of func2 in module A
    # all your func2 definitions belongs to module B
    # but this way it will fail with no error
    # just func2 will never be called

So, pls, read “comments” section of code example if u have not done it yet.
The workaround is that I need to define function func2 in module A and arity doesn’t matter.

I was also playing with on_definition hook to understand what’s going on and everything looks fine:

def on_def(_env, kind, name, args, guards, body), do: 
    IO.puts("Defining #{kind} named #{name}")

It shows me a definition of func2 right at the start where I was expecting it to be

Should I dive into sources of Elixir or is there some Unhidden Truth somewhere on this topic?
Is there something with compiler?

Yep, I forgot, module B just contains using macro with quoted definition of func2

use calls the macro __using__ on compile time of the mentioned module, this way code gets injected into the calling module.

Simplified use B gets replaced by whatever is in the returned quote block if Bs __using__.

This behaviour should be documented in the Kernel module under theuse` macro (or is it a special form?). Can’t search it for you as I’m on mobile data plan only…


In short, this:

defmodule A do
  use Foo, opts

Is exactly the same as:

defmodule A do
  require Foo

This is just sugar over it, see for yourself.


As I can understand from docs my code translates to require B plus call of B.__using__ macro. And that’s all. Then I should be able to use func2. Mb it is something to do with capturing function.