voger
I just finished “Programming Elixir” and now I am reading “Metaprogramming Elixir”. I can’t say I comprehend everything. I try to understand but I mostly pretend I understand and keep reading. While the previous examples were somehow manageable to follow I am stuck in recreating the if macro. Here is the code.
defmodule ControlFlow do
defmacro my_if(expr, do: if_block) do
if(expr, do: if_block, else: nil)
end
defmacro my_if(expr, do: if_block, else: else_block) do
quote do
case unquote(expr) do
result when result in [false, nil] -> unquote(else_block)
_ -> unquote(if_block)
end
end
end
end
Can someone please be patient and explain with simple words what is happening in this code?
I understand what quote and unquote do. I don’t understand why the two defmacro definitions.
Now that I write the question I realize the first defmacro is just for the case we have a simple if without else. The second defmacro handles the if/else case. But still I can’t understand how they work.
Trending in Questions
Hello everyone!
I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++.
Does anyone ...
New
I’m very new to Elixir, so sorry if this is a silly question:
I am wondering what happens to the mailbox of a process when the machine i...
New
How do I tell Hologram not to escape HTML when substituting into templates?
{Textile.render(@product.long_description)}
escapes the HTM...
New
Background
When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error:
* assembling mark...
New
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
Other Trending Topics
While waiting for other agents, I built a small CLI tool to remove the deps/_build/node_packages bloat from my system. It finds cache fol...
New
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #elixirconf-us
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #security
- #elixirconf-eu
- #processes
- #api
- #forms
- #hex











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
brainbag
If you understand that code like this:
is simply syntactic sugar for this:
and that is simply syntactic sugar for this:
and THAT is simply syntactic sugar for this:
then it might be a bit clearer. Remember that Elixir allows you to drop the List
[]if you’re passing in a keyword list.Some more detail: your first
my_iffunction takes two arguments, one of which is an explicit keyword list ofdo:. Sinceelseisn’t specified, it just passesnilin for theelse. That will run when you call code like this:Your second
my_iffunction does a case statement on the expression (somethingin my example). Inside of the case statement, it checks to see if the result evaluation is one offalseornil, and then returns theelse_block. Otherwise, it returns theif_block.You might check the “Keywords and maps” section of the documentation for further reference: http://elixir-lang.org/getting-started/keywords-and-maps.html
Hope that helps!
voger
Thanks. It did help a lot. Also I had to research more how “case” works in Elixir.
But I still have another problem with this code. I even copy pasted the from the book to my editor but still get the same result.
Here is the original code for the first my_if macro from the book
here is the result I get in iex
In lines 27 and 28 shouldn’t the if statement return nil?
brainbag
No, because you forgot something very important in your first definition.
If you do an
IO.inspectin that firstmy_if, what is the value ofexpr? It’s not what you think!When you figure that out, think about how you “transform” values so they’re usable by macros, and then look at your first
my_ifagain and see what you’re missing.voger
Wow thanks. I already tried to use unquote but I got errors because I didn’t wrap it first in “quote do”. The correct code is this
Thank you very much for your help.
kodepett
I faced the same issue will working through Metaprogramming Elixir. my_if returns true irrespective of the condition when invoked without the else block. I think it’s an errata, I supposed the line should be:
Below is the full snippet from the book on page 22, maybe @chrismccord can clarify, I could be wrong with my assumption.
chrismccord
Using the provided snippet from the book:
Are you sure your code is correct wherever you are running it?
kodepett
Hi, kindly find below - elixir v1.13.3
LostKobrakai
Shouldn’t the first macro body delegate to
my_if, notif?kodepett
I tried that and had an error
cannot invoke macro my_if/2 before its definitionchrismccord
Bah, yes it is indeed erratta. You’re the first to catch this in like 8 years!
If you give in a non AST literal, it’s always true because we are passing the ast to
ifin the first clause, not the expanded expression. So for example,1 > 2will be the ast tuple, instead of the boolean, which is always truthy. The code should have quoted the first clause. As @LostKobrakai said tho, a better example wouldn’t proxy to Elixir’s if in the first place: