Let’s imagine that Elixir didn’t have an if statement—that all it has is case. Although we’re prepared to abandon our old friend the while loop, not having an if statement is just too much to bear, so we set about implementing one.
We’ll want to call it using something like
| myif condition do |
| evaluate if true |
| else |
| evaluate if false |
| end |
We know that blocks in Elixir are converted into keyword parameters, so this is equivalent to
| myif condition, |
| do: evaluate if true, |
| else: evaluate if false |
Here’s a sample call:
| My.myif 1==2, do: (IO.puts "1 == 2"), else: (IO.puts "1 != 2") |
Let’s try to implement myif as a function:
| defmodule My do |
| def myif(condition, clauses) do |
| do_clause = Keyword.get(clauses, :do, nil) |
| else_clause = Keyword.get(clauses, :else, nil) |
| |
| case condition do |
| val when val in [false, nil] |
| -> else_clause |
| _otherwise |
| -> do_clause |
| end |
| end |
| end |
When we run it, we’re (mildly) surprised to get the following output:
| iex> My.myif 1==2, do: (IO.puts "1 == 2"), else: (IO.puts "1 != 2") |
| 1 == 2 |
| 1 != 2 |
| :ok |
When we call the myif function, Elixir has to evaluate all of its parameters before passing them in. So both the do: and else: clauses are evaluated, and we see their output. Because IO.puts returns :ok on success, what actually gets passed to myif is
| myif 1==2, do: :ok, else: :ok |
This is why the final return value is :ok.
We need a way of delaying the execution of these clauses. This is where macros come in. But before we implement our myif macro, we need a little background.