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

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
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
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement