voger

voger

Can someone please explain this 'my_if' code in the Metaprogramming Elixir book?

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.

First 10 of 11 Posts Switch mode

brainbag

brainbag

If you understand that code like this:

if something do
  x
else 
  y
end

is simply syntactic sugar for this:

if something, do: x, else: y

and that is simply syntactic sugar for this:

if(something, [do: x, else: y])

and THAT is simply syntactic sugar for this:

if(something, [{:do, x}, {:else, y}])

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_if function takes two arguments, one of which is an explicit keyword list of do:. Since else isn’t specified, it just passes nil in for the else. That will run when you call code like this:

my_if x == 2 do
  x
end

# or
my_if(x == 2, do: x)

# which, without the sugar, is actually this:
my_if(x == 2, [{:do, x}])

Your second my_if function does a case statement on the expression (something in my example). Inside of the case statement, it checks to see if the result evaluation is one of false or nil, and then returns the else_block. Otherwise, it returns the if_block.

my_if x == 2 do
  x
else
  y
end

# or 
my_if(x == 2, do: x, else: y)

# which, without the sugar, is actually this:
my_if(x == 2, [{:do, x}, {:else, y}])

# and this is, conceptually, what your my_if function does
case x == 2 do 
  false -> y
  nil -> y
  _ -> x
end

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

voger OP

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

defmacro my_if(expr, do: if_block), do: if(expr, do: if_block, else: nil)

here is the result I get in iex

iex(25)> c "if_recreated.exs"                                        
warning: redefining module ControlFlow (current version loaded from Elixir.ControlFlow.beam)
  if_recreated.exs:1
[ControlFlow]
iex(26)> require ControlFlow                                         
ControlFlow
iex(27)> ControlFlow.my_if 2 == 1, do: "correct"                     
"correct"
iex(28)> ControlFlow.my_if 2 == 1, do: "correct"
"correct"
iex(29)> ControlFlow.my_if 2 == 1, do: "correct", else: "not correct"
"not correct"
iex(30)> 

In lines 27 and 28 shouldn’t the if statement return nil?

brainbag

brainbag

No, because you forgot something very important in your first definition. :slight_smile:

If you do an IO.inspect in that first my_if, what is the value of expr? 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_if again and see what you’re missing.

voger

voger OP

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

defmacro my_if(expr, do: if_block) do
  quote do
    if unquote(expr), do: unquote(if_block), else: nil
  end
end

Thank you very much for your help.

kodepett

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:

defmacro my_if(expr, do: block) do
    quote do
      if unquote(expr), do: unquote(block), else: nil
    end
  end

Below is the full snippet from the book on page 22, maybe @chrismccord can clarify, I could be wrong with my assumption.

defmodule ControlFlow do
   defmacro my_if(expr, do: if_block), do: if(expr, do: if_block, else: nil)
   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
chrismccord

chrismccord

Creator of Phoenix

Using the provided snippet from the book:

iex(7)> ControlFlow.my_if false, do: :here
nil
iex(8)> ControlFlow.my_if true, do: :here 
:here

Are you sure your code is correct wherever you are running it?

kodepett

kodepett

Hi, kindly find below - elixir v1.13.3

iex(92)> ControlFlow.my_if false, do: :here  
nil
iex(93)> ControlFlow.my_if true, do: :here 
:here
iex(94)> ControlFlow.my_if 1 > 2, do: :here
:here
iex(95)> ControlFlow.my_if 1 == 2, do: :here
:here
LostKobrakai

LostKobrakai

Shouldn’t the first macro body delegate to my_if, not if?

kodepett

kodepett

I tried that and had an error cannot invoke macro my_if/2 before its definition

chrismccord

chrismccord

Creator of Phoenix

Bah, yes it is indeed erratta. You’re the first to catch this in like 8 years! :slight_smile:

If you give in a non AST literal, it’s always true because we are passing the ast to if in the first clause, not the expanded expression. So for example, 1 > 2 will 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:

iex(2)>    
nil
iex(3)> defmodule ControlFlow do
...(3)>   defmacro my_if(expr, [{:do, if_block} | _] = opts) do
...(3)>     quote do
...(3)>       case unquote(expr) do
...(3)>       result when result in [false, nil] -> unquote(opts[:else])
...(3)>       _ -> unquote(if_block) end
...(3)>     end
...(3)>   end
...(3)> end
{:module, ControlFlow,
 <<70, 79, 82, 49, 0, 0, 6, 208, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 183,
   0, 0, 0, 20, 18, 69, 108, 105, 120, 105, 114, 46, 67, 111, 110, 116, 114, 
   111, 108, 70, 108, 111, 119, 8, 95, 95, 105, ...>>, {:my_if, 2}}
iex(4)> require ControlFlow
ControlFlow
iex(5)> ControlFlow.my_if 1 > 2, do: :here
nil
iex(6)> ControlFlow.my_if 3 > 2, do: :here
:here
iex(7)> 

Where Next?

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
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
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
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
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 &amp; Solve. They are GUI (Emerge) and State management (S...
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
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement