svilen
Author of Concurrent Data Processing in Elixir
I have a bunch of functions in a module, that I’d like to dynamically add to a __using__ macro:
defmodule A do
defmacro __using__(_opts) do
quote do
# Dynamically add
# def foo(), do: "Foo"
# and any other functions.
end
end
def foo(), do: "Foo"
end
My goal is to have the functions accessible on the original module, inject them into the module that’s using it via use and make them overridable:
I’m just starting with metaprogramming so I’d appreciate any solutions or pointers in the right direction. Thanks!
Trending in Questions
One of my LiveView view’s has a very heavy javascript dependency so I want to avoid loading it for every page on the site, while still us...
New
Hi all!
I know that quite a few people use Phoenix LiveView but it is hard to get a more up-to-date list of folks/companies using it. I ...
New
Hello everyone!
I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++.
Does anyone ...
New
I’m very new to Elixir, so sorry if this is a silly question:
I am wondering what happens to the mailbox of a process when the machine i...
New
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
Background
When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error:
* assembling mark...
New
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hi everyone :waving_hand:
I’ve been working on PhiaUI, an open-source UI component library for Phoenix LiveView, and I wanted to share i...
New
On November 23, 2026, an unauthorized third party exploited a vulnerability in hex.pm (CVE-2026-129514, CVSS 10.0) that let them run comm...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #ai
- #authentication
- #elixirconf-us
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-eu
- #distillery
- #security
- #processes
- #api
- #forms
- #hex











Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
jswny
I’m pretty sure you can just
importthe parent module of the__using__macro withimport Ainside the macro. For example, the Phoenix 1.3 generators create the followingDataCasemodule:So, all of the
MyApp.DataCasefunctions will be auto imported into the scope of any module which callsuse MyApp.DataCase.idi527
Kernel — Elixir v1.20.2 has an example similar to what you want:
And then in the shell
svilen
Thanks for everyone’s replies.
@jswny I did try that but doesn’t work as you’d expect:
You can access
Is this
A.foo()but notB.foo()using do.. endDSL doing something special, compared todefmacro __using__(_)?@idi527
defoverridableis indeed what I need to use to make an injected function overridable, but that’s only one part of the puzzlekip
importmakes the functions fromAavailable in moduleBbut it doesn’t re-export them. Therefore functions inBcan invoke the imported functions fromAbut you can’t call them from outsideB. Its nothing to do withuse, its howimportworks.Overall, your last example isn’t a strong justification for
use. Justimportwould be preferred because the intent is clearer. But I recognise your use case if presumably more complex that justimport.kip
If basically you want to delegate a bunch of functions to another module, have them available as public functions on the new module and also make them overridable then perhaps the following would work? Its not considered good practise to do defining lots of functions in a
quoteblock.svilen
@kip Awesome, using
defdelegateachieves the effect that I wanted! I’ve come acrossdefdelegatebefore, but it didn’t occur to me that it would work just fine withdefoverridable. Now it makes sense.You were right in your previous post — I oversimplified my use case just for convenience. Ultimately I was wondering if this could work on multiple levels, e.g.
If module D comes around and wants to inherit/extend module C, it doesn’t need to know about A or B at all. This approach tightly couples all the modules, but could be useful in some cases where it can be tolerated.
Thanks a lot for your help!
kip
I sort of understand your intent but, to me (and maybe only me), this smells a bit of “making magic”. And the language “inherit/extend” makes it seem even more so.
I’ve found that a combination of partial
importing of functions and the occasional use ofdefdelegateis a lot clearer to me because its explicit. One of the most frustrating things is to see a function call to a function whose definition you can’t find. The multiple indirectusees would, for me, make the intent obscure and quite frustrating.LostKobrakai
To whom is that useful? You still need A and B to actually do things, so why not make that clear to the consuming code. The only reason for this I can imagine is that you want to own the implementation and do (or at least be able to do) changes on how A or B works. But then defdelegegate are imho the best option to be used within your intermediate consumer. Hiding the defdelegates in the implementation providing module is always a great indirection, which should have convincing reasoning for it’s usage.
svilen
@kip @LostKobrakai Yep, I definitely don’t disagree with you and
importis much simpler and clearer for the majority of cases. I was purely interested in how could this be achieved from a metaprogramming standpoint. Whether this is a good or bad idea is a different topic, which I don’t intend to start