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
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
sodapopcan
Honestly this looks fine to me—it’s nice and explicit. It’s not just the
whenclause that’s different butpayload: unquote(pattern)vpayload: 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
christhekeele
Consider the (only semi-public but very stable)
:elixir_utils.extract_guards/1function 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:
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:
sodapopcan
He did not disappoint, but also made me more confident in my resolve
TIL about
:elixir_utils.extract_guardswhich 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
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
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
@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.