roflbobl

roflbobl

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

First 7 of 7 Posts Switch mode

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:

christhekeele

christhekeele

Consider the (only semi-public but very stable) :elixir_utils.extract_guards/1 function from Elixir’s compiler. It takes in Macro AST of an expression, and returns a two-tuple of that expression with top-level guards removed, and a list of the guards extracted. Its implementation is similar to yours but handles multiple guards correctly (a rarely used feature in Elixir code, but syntactically valid).

Ex:

payload = quote(do: foo)
{pattern, guards} = :elixir_utils.extract_guards(payload)
#=> {{:foo, [], Elixir}, []}
payload = quote(do: foo when fizz)
{pattern, guards} = :elixir_utils.extract_guards(payload)
#=> {{:foo, [], Elixir}, [{:fizz, [], Elixir}]}
payload = quote(do: foo when fizz when buzz)
{pattern, guards} = :elixir_utils.extract_guards(payload)
#=> {{:foo, [], Elixir}, [{:fizz, [], Elixir}, {:buzz, [], Elixir}]}

You can then rebuild the guard AST around a new expression for insertion where desired without thinking about if there are zero or more by reducing over them:

new_guarded_expression = quote(do: bar)
Enum.reduce(guards, new_guarded_expression, fn guard, expression ->
  {:when, [], [expression, guard]}
end)
|> Macro.to_string
#=> "(bar when fizz) when buzz"
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

christhekeele

christhekeele

I’m inclined to agree if this macro is intended for an application. You control the version of Elixir at all points in time, control the calling code and can proceed with confidence that guards are not nested, and the simpler literal AST manipulation is more intention-revealing.

If it’s meant for a library there’s a small benefit in using these compiler functions (aside from handling nested guards): you’ll also know whatever version of Elixir the end user is running, your code will work for that version, whatever “correct” semantics of extracting guards are like for that version. (Not that the semantics of guards have ever changed or are likely to throughout Elixir ~> 1.0.)

sodapopcan

sodapopcan

Yep, I’m glossing over a lot here, especially since OP’s code is clearly a test assertion. There is absolutely a huge difference in what’s acceptable in library v. application code and I was assuming this was for the latter (even if it is library-esque code within application code).

roflbobl

roflbobl OP

@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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement