7stud

7stud

Metaprogramming Elixir book: what does this mean?

On p. 6, there is this example:

defmodule Math do

  # {:+, [context: Elixir, import: Kernel], [5, 2]}
  defmacro say({:+, _, [lhs, rhs]}) do   #line 4
    quote do
      lhs = unquote(lhs)
      rhs = unquote(rhs)
      result = lhs + rhs
      IO.puts "#{lhs} plus #{rhs} is #{result}"
      result
    end
  end

  # {:*, [context: Elixir, import: Kernel], [8, 3]}
  defmacro say({:*, _, [lhs, rhs]}) do   #line 15
    quote do
      lhs = unquote(lhs)
      rhs = unquote(rhs)
      result = lhs * rhs
      IO.puts "#{lhs} times #{rhs} is #{result}"
      result
    end
  end
end

The text says:

Let’s break down the code. Since we know macros receive the AST representation of the arguments we pass to them, we pattern matched directly on the AST to determine which say definition to invoke. On lines 4 and 15, we can see that macros , like functions, can have multiple signatures. Having the example representation from our quoted results allowed us to easily bind the left- and right-hand side values to variables and print a message accordingly. Too complete the macro, we used quote to return an AST for the caller to replace our Math.say invocations. Here we also used unquote for the first time…

What in the world does:

Having the example representation from our quoted results allowed us to easily bind the left- and right-hand side values to variables and print a message accordingly.

mean?! I recognize the words as English, but they have no intelligible meaning to me.

Most Liked

7stud

7stud

Ahh, I figured it out. The phrase “Having the example representation from our quoted results” refers to:

 # {:+, [context: Elixir, import: Kernel], [5, 2]}

The “quoted results” are on the previous page where the book used quote() on some math expressions.

chrismccord

chrismccord

Creator of Phoenix

can confirm :slight_smile:

blatyo

blatyo

Conduit Core Team

That is my understanding as well. Just for some confirmation.

If you’re looking at the commented out representation of the AST and then imagine how that will be matched against the function head, it’s easy to see how 5 and 2 will be bound to the right-hand and left-hand variables.

Last Post!

7stud

7stud

lol. Thanks!

I would, however, suggest a rewrite. I read that sentence 20 times, and I couldn’t figure out what it meant until just after I posted my question (as sometimes happens!). Maybe something as simple as:

With the AST in the comments as a guide we were easily able to bind the left- and right-hand side values to variables and print a message accordingly .

Where Next?

Popular in Chat/Questions Top

asfand
Hi Everyone, I am a student and know basics of web development, used some php and ruby, but I am not an expert in any. I want to learn E...
New
New
Fl4m3Ph03n1x
Background After following the communitiy suggestion, I bought the Elixir in Action 2nd Edition book and I am about to finish it now. I ...
New
Chawki
Hi,i’m new to elixir. i’m searching elixir small programs to try it out my self,Is any good resources out there? Thank you.
New
New
satoru
I’m working on the “Bob” exercise on the Elixir Track in Exercism. I am testing for uppercase letter with this simple check: c in ?A..?Z...
New
stevensonmt
I’d like to provide my review of the Elixir Course module from Groxio. I have some criticisms but I’d like to start with the positives. ...
New

Other popular topics Top

hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44608 311
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement