params icon indicating copy to clipboard operation
params copied to clipboard

Compile error when using defparams macro in module that uses Phoenix.Component

Open mpinkston opened this issue 2 years ago • 3 comments

Phoenix.Component overrides the def and defp macros to annotate function heads with some pattern matching. I believe because defparams creates a new method by calling Module.eval_quoted/2 this is somehow evaluating the ast returned by Phoenix.Component's def macro prematurely(? - tbh, I don't grok this perfectly yet)

But the error can be seen with code like the following:

defmodule MyMod do
  use Phoenix.Component
  use Params

  defparams fleeno %{some: :string}

  def hi do
    IO.puts("Hello")
  end
end
error: cannot invoke remote function Phoenix.Component.Declarative.__pattern__!/2 inside a match
  nofile:1: MyMod.util_2/1

Is there a reason I'm not understanding that the defparams macro is using Module.eval_quoted/2 rather than just defining the method directly?

In other words, can't

  Module.eval_quoted(__MODULE__, quote do
     def unquote(name)(params) do
       unquote(module_name).from(params)
     end
  end)

Just be written as

  def unquote(name)(params) do
    unquote(module_name).from(params)
  end

In my own experiments it seems to work fine, but I'm not sure if I'm missing something.

(Of course, I could just call the macro before I use Phoenix.Component, but perhaps there's a more comprehensive fix)

mpinkston avatar Dec 19 '23 02:12 mpinkston

Seeing this too, curious if you ever got around it? @mpinkston

Dalepalmer avatar Dec 10 '24 20:12 Dalepalmer

Seeing this too, curious if you ever got around it? @mpinkston

@Dalepalmer Sorry for the late reply, I just called use Params first to get around this; but Module.eval_quoted has been deprecated in recent versions of Elixir, so it's going to have to be revisited eventually

  use Params
  use Phoenix.Component

mpinkston avatar Feb 26 '25 02:02 mpinkston

@mpinkston no worries, we ended bringing in the code and adjusted it to play nice here (using your suggested path). Good to know about Module.eval_quoted and have that on our radar.

Dalepalmer avatar Feb 26 '25 03:02 Dalepalmer