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
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 ![]()
2
hykw
Popular in Discussions
A big advantage to Elixir is all the distributed goodness but for many applications running on multiple nodes having integrated Etcd, Zoo...
New
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help.
Where are they similar?
Where do they differ the m...
New
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
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
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
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
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
Following https://github.com/tbrand/which_is_the_fastest |>
https://raw.githubusercontent.com/tbrand/which_is_the_fastest/master/imgs...
New
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
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
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I’m writing a test for one of the...
New
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
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
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
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
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
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
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
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
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
- #hex









