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

Tee
can someone please explain to me how Enum.reduce works with maps
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

We're in Beta

About us Mission Statement