edmundobiglia
Problem with function to dynamically define modules
Hi there! I’m trying to create a function that dynamically defines a module based on the passed argument, like so:
defmodule Test do
def define_module(module_name, value) do
defmodule module_name do
def run, do: %{value: value}
end
end
end
However, when I run Test.define_module(:Hello, "world"), I get this error:
** (CompileError) iex:9: undefined function value/0 (expected :Hello to define such a function or for it to be imported, but none are available)
(elixir 1.13.3) src/elixir_locals.erl:115: anonymous fn/4 in :elixir_locals.ensure_no_undefined_local/3
(elixir 1.13.3) src/elixir_erl_compiler.erl:12: anonymous fn/2 in :elixir_erl_compiler.spawn/1
On the other hand, the following does work:
defmodule Test do
def define_module(module_name, value) do
defmodule module_name do
@value value
def run, do: %{value: @value}
end
end
end
Test.define_module(:Hello, "world")
Is there a way to use the argument as the return of the inner function without making it a module attribute?
Thanks a lot in advance! ![]()
Marked As Solved
al2o3cr
def doesn’t capture its environment, so it won’t work directly.
You could do some AST surgery on what def produces:
{:def, [context: Elixir, import: Kernel],
[
{:run, [if_undefined: :apply, context: Elixir], Elixir},
[do: {:%{}, [], [value: PUT_THE_VALUE_HERE]}]
]}
but the pattern of “interpolate this compile-time value into the AST” is frequent enough that there’s a built-in mechanism for doing it… module attributes ![]()
Also Liked
NobbZ
- You probably want to use macros rather than functions.
- You need to use
unquotewithin a quoted context.
1
Popular in Questions
If I have a post route which an argument:
post /my_post_route/:my_param1, MyController.my_post_handler
How would get the post params ...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
Hey all,
I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
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
In templates/appointment/index.html.eex:
<%= for appointment <- @appointments do %>
<tr>
<td><%= appoi...
New
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
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
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
Other popular topics
If I have a post route which an argument:
post /my_post_route/:my_param1, MyController.my_post_handler
How would get the post params ...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
Hi!
In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir?
Searched the docs for ip address and the web, no good results.
Thanks!
New
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New








