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
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
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 t...
New
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service.
Currently when I de...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
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
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
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size?
Thanks
New
Other popular topics
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
In Ruby, I can go:
User.find_by(email: "foobar@email.com").update(email: "hello@email.com")
How can I do something similar in Elixir? ...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
Hi all,
I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I'm trying to use Postg...
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
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New







