markdev

markdev

Are macros lazily evaluated?

I am watching this talk by Jesse Anderson at ElixirConf about how macros work. He creates a macro (if statement), then contrasts it with a functional example.

Macro Version:

defmodule MyMacros do
  defmacro my_if(condition, do: do_clause, else: else_clause) do
    quote do
      case unquote(condition) do
        true -> unquote(do_clause)
        false -> unquote(else_clause)
      end
    end
  end
end

Function version:

defmodule MyFunctions do
  def my_if(condition, do: do_clause, else: else_clause) do
     case condition do
        true -> do_clause
        false -> else_clause
      end
   end
end

If you were to call my_if 1 + 2 == 3, do: "this", else: "that", you get the same result from either module. But if evaluation order matters in the do/else blocks, the behavior is different.

iex(17)> MyMacros.my_if 1 + 2 == 3, do: IO.puts("this"), else: IO.puts("that")
this
:ok
iex(18)> MyFunctions.my_if 1 + 2 == 3, do: IO.puts("this"), else: IO.puts("that")
this
that
:ok

Is this considered an example of lazy evaluation, or is this deferred evaluation typically described some other way?

Marked As Solved

tmbb

tmbb

Your macro isn’t perdorming any kind of evaluation, lazy or strict. The macro works because it expands into a case special form, which does allow you to pick branches. In a sense I guess the case macros is doing something that can be considered lazy evaluation.

But your if macro is not performing lazy evaluation. It’s just expanding into another syntax form.

Also Liked

blatyo

blatyo

Conduit Core Team

A macro is replaced with the AST that it returns. So, this:

MyMacros.my_if 1 + 2 == 3, do: IO.puts("this"), else: IO.puts("that")

Turns into this at compile time:

case 1 + 2 == 3 do
  true -> IO.puts("this")
  false -> IO.puts("that")
end

Like @tmbb said, case is lazy. Not the macro.

The function appears non-lazy, because the arguments have to be evaluated before they are passed to MyFunctions.my_if/2.

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

We're in Beta

About us Mission Statement