feld
I have this situation where I have a list of modules in a module attribute to be used for another purpose and I also want to “use” them, so I thought it would be nice to have a single place in the module to enable/disable their functionality. Unfortunately I have been unable to find a way to make it work, but it doesn’t provide me any errors.
Is there any way to make this work as I expect?
defmodule Example do
@mods [
Example.One
Example.Two
]
def something() do
# When called, enumerate over @mods here for another purpose
end
# this doesn't work
for mod <- @mods, do: quote do: use unquote(mod)
end
It doesn’t error, but it definitely is not calling the __using__ of that module. Explicitly setting the use works, even if you set it after this, so it’s definitely not working.
Any thoughts?
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Hello,
I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter).
The diffic...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
Other Trending Topics
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
New
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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #elixirconf-us
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
zachdaniel
is equivalent to
Note that the
quote dothere just causes the AST A.K.A “quoted code” to be returned. Nothing is actually evaluating it.Try this (haven’t verified that it works). In a different module, make a macro called
use_all.Then in your module you can use
zachdaniel
There is a simpler way that is perhaps a bit less hygienic as its typically recommended to avoid
Code.eval_quoted, but you could do this:feld
Thanks! I knew I was on the right track and I was staring at the source code for
usein the Kernel but hadn’t remembered that the AST I was generating wasn’t being evaluated…I haven’t tested the more complex solution but I’ll give it a spin tomorrow and report back for future readers.
zachdaniel
Honestly I think it may just be the right way. I’m not sure why I was thinking one should avoid
Code.eval_quoted, but I think that usage of it is perfectly reasonable.sodapopcan
I can’t get this one to work. Isn’t this going to result in each
I actually can’t get the first example working either, says that
usebeing scoped tofor’s block? I have a poor mental model of usingquotedirectly in a module body, though I was trying other stuff earlier (somewhat akin to your first answer, though not quite) and no luck there either@modsis unused.feld
Oh that would be funny if I found a bug in the compiler or something. I’m already using that @mods equivalent in another function so it wouldn’t error as unused. See if that solves it for you?
zachdaniel
You’re right
Here is a working example:
This demonstrates that you can use the list again later w/
@using_modules, and things likeimportaffect the top level scope.sodapopcan
Yes, sorry, I shouldn’t have focused on the warning. It doesn’t accomplish the
usefor me is the main thing. I could be doing it wrong though, it’s getting a bit late for me.sodapopcan
Oh, this is clever! This code (without the
@using_modulespart) is very close to what I had working with the problem being that it failed passing it a module attribute. Instead ofexpand_literals, though, I usedexpand_once. Is there a big difference here? I never quite understood the docs ofexpand_literalsthough it actually sounds like might be equivalent in this case? Both have to do with expanding aliases properly?zachdaniel
I think they’re equivalent here, yeah.