PatrickSachs

PatrickSachs

Inject variable into macro function body

My termininology with the question title is probably all over the place, but I’m really struggling on how to phrase it.

What I essentially want to do, is something like plug does in its “get”/“post”, etc. macros: It injects the “conn” variable without having to declare it anywhere.

I have this macro:

defmacro socket(matcher, do: expr) do
  quote do
    def websocket_handle({:json, unquote(matcher) = json}, state) do
      try do
        unquote(expr)
      rescue
        e -> Logger.error("Websocket Error - #{inspect(e)}")
      end
    end
  end
end

and use it like this:

socket %{"action" => "join_chat", "id" => id} do
  my_fun(id)
  my_fun(json) # :(
  my_fun(state) # :(
  {:reply, %{}, state}
end

Now, the “id” variable declared in the matcher is readily available, however “json” and “state” are not. It’s certainly related to scope, but I have tried all sorts of different things - to no avail.

The “var!” macro didn’t really help because that required me to declare the variable first, which is the exact opposite of what I want. I looked at the plug source code, but I had a hard time following it.

Is what I want to do feasable with a small macro? If yes, how - if no, what are my alternatives for giving access to the “json” and “state” variables?

Thank you for your time!

Marked As Solved

OvermindDL1

OvermindDL1

All variables have to be declared before use, this is what plug does as well. You should be able to just replace the variable names in the function definition inside the quote I’d imagine, to be something like this:

def websocket_handle({:json, unquote(matcher) = unquote(Macro.var(:json, nil))}, unquote(Macro.var(:state, nil))) do

Does that not work?

Also Liked

PatrickSachs

PatrickSachs

This seems to work really well! Thank you very much for this - you been a massive time saver.

Last Post!

PatrickSachs

PatrickSachs

This seems to work really well! Thank you very much for this - you been a massive time saver.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
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
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

Other popular topics 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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
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
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

We're in Beta

About us Mission Statement