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

Darmani72
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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
mgjohns61585
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
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
minhajuddin
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
LegitStack
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
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
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
shijith.k
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 Top

Darmani72
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
jason.o
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
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
dblack
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
rms.mrcs
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
jononomo
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement