roflbobl

roflbobl

Adding guard to macro that just calls another function

I have a problem, where i have a macro the calls another function. I want to be able to add optional guards to my macro, and call the nested function with those guards. I have a solution, but i feel like it is verbose and there is something i dont know of. Its the use of case do that feels clunky, and the only difference is the when clause.

Example

  defmacro assert_push(
             event,
             payload,
             timeout \\ 100
           ) do
    case payload do
      {:when, _, [pattern, guard]} ->
        quote do
          assert_receive %Phoenix.Socket.Message{
                           event: unquote(event),
                           payload: unquote(pattern)
                         }
                         when unquote(guard),
                         unquote(timeout)
        end

      _ ->
        quote do
          assert_receive %Phoenix.Socket.Message{
                           event: unquote(event),
                           payload: unquote(payload)
                         },
                         unquote(timeout)
        end
    end
  end

Most Liked

sodapopcan

sodapopcan

Honestly this looks fine to me—it’s nice and explicit. It’s not just the when clause that’s different but payload: unquote(pattern) v payload: unquote(payload). What you have is nice and explicit and I think DRYing it up would probably be more confusing (and hence a great case of “when DRY is too DRY”).

Although I see Chris K. is replying and he a notorious committer of macro crimes, so I’m interested to see what he’ll suggest :smiley:

sodapopcan

sodapopcan

He did not disappoint, but also made me more confident in my resolve :grin: TIL about :elixir_utils.extract_guards which is very cool. But for what it’s worth, in a tense debugging session (or even just coming across it randomly in while trying to understand a system) I would much rather see your code.

christhekeele

christhekeele

ezgif-2296daa4eccd89

Last Post!

roflbobl

roflbobl

@christhekeele @sodapopcan this is actually from Phoenix, so it is library code. I wanted to add the functionality as stated in my post for testing.

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
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
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
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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
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