sodapopcan

sodapopcan

Using `~p` dynamically inside a macro

I’m having a bit of trouble wrapping my brain around how I would pass a dynamic fragment to ~p inside a macro:

defmacro __using__(opts) do
  @path opts.path

  quote do
    def handle_event("event", _, socket) do
        path = ~p"/#{@path}"

        # ...
        {:noreply, socket}
    end
  end
end

I’ve tried a fair number of functions from the Macro module both inside, namely escape and expand/expand_once which are the ones I though would do it as well as unquote (and even quote for good measure). I understand that passing an argument to the ~p macro is going to quote it and when it’s called already inside a quote block, well, uh… oh boy :face_with_spiral_eyes: As I have it does produce the correct route but of course gives a console warning. I feel like this question must have been answered before but I can’t find it and my brain hurts. At the very least I’m treating this forum as a rubber duck but any help would be much appreciated.

Most Liked

LostKobrakai

LostKobrakai

That’s likely the clue here. The compile time validation doesn’t see ~p"/some-path", but ~p"/#{"some-path"}". Evaluate both and it’ll be the same, but their AST/the code isn’t. The latter is not using a static string, even if the dynamic input takes a static value.

You likely want this: path = sigil_p(unquote("/#{path}")) – build the static path before unquoting the result into the sigil macro call.

0xG

0xG

Hmm, I think sigil_p/2 pattern matches against ast and unquoting that expression results in fully evaluated binary.

I bet there would be a more intuitive solution but this works:

defmodule A do
  defmacro __using__(opts) do
    path = opts[:path]

    quote do
      use Phoenix.Component

      def handle_event("event", _, socket) do
        path = sigil_p(unquote({:<<>>, _meta = [], ["/#{path}"]}), [])

        # ...
        {:noreply, socket}
      end
    end
  end
end

defmodule B do
  use A, path: "my-path"
end
warning: no route path for InformedCMSWeb.Router matches "/my-path"
  iex:112: B.handle_event/3

So I passed the simplified version of desired ast:

iex> quote(do: "/#{path}")
{:<<>>, [],
 [
   "/",
   {:"::", [],
    [
      {{:., [], [Kernel, :to_string]}, [], [{:path, [], Elixir}]},
      {:binary, [], Elixir}
    ]}
 ]}
sodapopcan

sodapopcan

Hey hey, thanks for the response and welcome to the forum!

I’ve tried your suggestions and I’m still getting the warning. Are you seeing it? Maybe I messed up still. My problem was never that it didn’t compile—it compiled and produced the correct path, but it failed to be verified. The warning is no route path for MyAppWeb.Router matches "/#{"some-path"}" so I need to be able to pass the ~p macro and already expanded variable… which is what I’m having a hard time wrapping my head around.

Last Post!

sodapopcan

sodapopcan

Thanks for your help, @0xG and @LostKobrakai. I’m ultimately going with a slightly more verbose but more flexible (and less brain-bendy for me) solution. I though I was only going to be dealing with URLs in the form of: ~p/resource/#{resource} but it turns out that is not the case and don’t want to get too crazy! Thanks again!

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36654 110
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement