FabienHenon

FabienHenon

CompileError when trying to generate a module and a function with Module.create

Hi,

I’m trying to generate a module with a function at runtime (from an Phoenix LiveView handle_params function), but I’m not able to make my code work.

Here is a sample code that fails:

page = "TestPage"
mod_name = Module.concat(MyProject.Pages, page)

Module.create(
      mod_name,
      quote do
        def render(assigns) do
          :ok
        end
      end,
      line: 1,
      file: "no_file.exs"
    )

When I run this code (inside a handle_params function from a LiveView) I get this CompileError:

error: cannot invoke remote function Phoenix.Component.Declarative.__pattern__!/2 inside a match
└─ no_file.exs:1: MyProject.Pages.TestPage.render/1

What does it mean?
Am i missing something?

My code seems pretty simple, but still, it does not work.
I’m not a macro expert, maybe there is something I misunderstood.

When I try the same code with no argument for the render function, it works.
But as soon as I add an argument, it fails.

Thank you for your help

Marked As Solved

frankdugan3

frankdugan3

Try this in your macro:

def my_render(var!(assigns)) do

Also Liked

D4no0

D4no0

Yeah, so liveview render callbacks are kinda special, they are supposed to return a template, not an atom as in your case.

Without a specific example of how you are using it, it will be hard to determine what you are doing incorrectly. It does seem like you are missing a huge amount of prerequisites, like imports for views/liveviews/components, etc. in your module declaration.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
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
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
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
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
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

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
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
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement