feld
Iteratively calling "use" on a list of modules?
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?
Marked As Solved
feld
I’m going to leave this as the solution: “Just don’t try this. It’s not worth it.”
Also Liked
sodapopcan
That’s why he set the module attribute within the macro.
The problem is that use, as well as import and alias, are scoped to the block they are called in, so you have to get around that somehow and it’s a little tricky.
For example:
defmodule Foo do
if true do
import String
capitalize("foo") # all good!
end
capitalize("foo") # Error: `capitalize` doesn't exists
end
zachdaniel
I think they’re equivalent here, yeah.
BartOtten
Maybe this solution was not the right one, but there might be others. Maybe we can find one in another direction?
Edit: I’m on mobile, @sodapopcan is in meeting. Frustrating hours ![]()
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
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance










