feld

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

feld

I’m going to leave this as the solution: “Just don’t try this. It’s not worth it.”

Also Liked

sodapopcan

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

zachdaniel

Creator of Ash

I think they’re equivalent here, yeah.

BartOtten

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 :sweat_smile:

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; somethi...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
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
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 t...
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
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
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41989 114
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement