ar7max

ar7max

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
    end

    # 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
end

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

Most Liked

hauleth

hauleth

In short, this:

defmodule A do
  use Foo, opts
end

Is exactly the same as:

defmodule A do
  require Foo
  Foo.__using__(opts)
end

This is just sugar over it, see for yourself.

NobbZ

NobbZ

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…

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement