hykw

hykw

How to set first-class module dynamically?

I have a logic like this. All the modules have the same patterns in their names(Foo.Module_[NUMERIC]). Can I set the module into f more simply like: f = Foo.Module_#{id}; f.get() ?

id = 2

f = case id do
  "1" => Foo.Module_1
  "2" => Foo.Module_2
  "3" => Foo.Module_3
end

f.get()
defmodule Foo.Module_1 do
  def get(), do: SOME-LOGICS-HERE
end
defmodule Foo.Module_2 do
  def get(), do: SOME-LOGICS-HERE
end
defmodule Foo.Module_3 do
  def get(), do: SOME-LOGICS-HERE
end

Most Liked

vidalraphael

vidalraphael

That’s a somewhat unorthodox approach, but it’s possible:

# Will raise if there's an attempt to use a number
# that doesn't have a corresponding module
mod = String.to_existing_atom("Elixir.Foo.Module_#{num}")

# Just call afterwards
mod.get()

The use of the String.to_existing_atom/1 is necessary because string to atom conversion isn’t safe in Erlang/Elixir, as atoms are not garbage collected, and filling up the atoms table is one of the ways to bring down the BEAM. Since the modules should be defined in the first place, the atoms that reference their names will already exist.

Hope it helps you :smiley:

hykw

hykw

It worked perfectly as I expected.
Thank you.

Where Next?

Popular in Discussions Top

andre1sk
A big advantage to Elixir is all the distributed goodness but for many applications running on multiple nodes having integrated Etcd, Zoo...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
Fl4m3Ph03n1x
Background This question comes mainly from my ignorance. Today is Black Friday, one of my favorite days of the year to buy books. One boo...
New
AstonJ
If a newbie asked you about Phoenix Contexts, how would you explain the basics to them? Feel free to be as concise or in-depth as you li...
New
ricklove
I was just introduced to Elixir and Phoenix. I was told about the 2 million websocket test that was done 2 years ago. From my research, t...
New
lorenzo
Hey everone! I created a prototype for my app using Nodejs for the api. But the framework I chose wasnt great (in general theresnt any g...
New
mbenatti
Following https://github.com/tbrand/which_is_the_fastest |> https://raw.githubusercontent.com/tbrand/which_is_the_fastest/master/imgs...
New
ejpcmac
I have discovered Nix last month and I am currently on my way to migrating to it—both on macOS at home and the full NixOS distrubution at...
New
und0ck3d
Hello everyone! A few days ago I’ve created a topic here about how people were creating CMSs with Elixir and Phoenix. I’ve been studying...
New

Other popular topics Top

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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43806 214
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement