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
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 the use` macro (or is it a special form?). Can’t search it for you as I’m on mobile data plan only…
Last Post!
ar7max
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










