edmundobiglia

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! :slight_smile:

Marked As Solved

al2o3cr

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

Also Liked

NobbZ

NobbZ

  1. You probably want to use macros rather than functions.
  2. You need to use unquote within a quoted context.

Where Next?

Popular in Questions Top

senggen
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
New
logicmason
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
Harrisonl
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
alice
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
ovidiubadita
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
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
sergio
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
_russellb
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
fireproofsocks
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
johnnyicon
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
ovidiubadita
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement