⚡ Helpful tip to debug Elixir macros!

Notes

When dealing with Elixir macros, we’re dealing with quoted expressions. And it’s sometimes hard to know what those evaluate to.

That’s why Elixir has the awesome helper Macro.to_string/1

iex> a = quote do: fn a -> a + 1 end
{:fn, [],
 [
   {:->, [],
    [
      [{:a, [], Elixir}],
      {:+, [context: Elixir, imports: [{1, Kernel}, {2, Kernel}]],
       [{:a, [], Elixir}, 1]}
    ]}
 ]}

As you can see, that’s a bit tough to read. And that’s a simple case!

Now let’s see what Macro.to_string/1 can do for us:

iex> Macro.to_string(a)
#=> "fn a -> a + 1 end"

Now that’s easy to read!

Want the latest Elixir Streams in your inbox?

    No spam. Unsubscribe any time.