5) ElixirConf 2017 - Don't Write Macros But Do Learn How They Work - Jesse Anderson

Here’s today’s talk:

ElixirConf 2017 - Don’t Write Macros But Do Learn How They Work - by @jessejanderson

You may have heard that “most of Elixir was written in Elixir” but what does that even mean, and how is that possible? Metaprogramming allows us to write code that writes code and, in addition to powering most of Elixir itself, has allowed for many great projects including Phoenix to exist. Even if you don’t have ambitions to create the next Phoenix, understanding metaprogramming basics and learning how the Elixir expressions work under the hood can lead to greater understanding of the code you write every day.

All talks are available in the Elixir Conf 2017 Talks List


I really enjoyed both this and Jesse’s previous talk at Erlang & Elixir Factory '17. He’s an entertaining speaker who gives really great breakdowns of these aspects of Elixir that newcomers to the language can get tripped up on.

I agree that it was a solid introductory presentation. I found myself wishing it went a touch deeper, but I did enjoy it.

Having not delved into the AST form of Elixir I felt that this was a solid presentation on how to use macros. I especially liked the use of pattern matching with defmacro since I can see many ways that could be useful. It was definitely well presented and I liked the use of color/fading to highlight portions of code.

However, I do have a question. Why was the two element tuple made a literal but an empty tuple ({}), and a single-element tuple ({:example}) are not literals? It seems like it would be easier/more consistent for all tuples to be treated equally. If I had to take a stab at it, I would guess that it is somehow related to S-expressions and Lisp.

The reason that two-element tuples are special is so that keyword lists can be special. See the mailing list thread where José confirms it.

Zero and one item tuples could be special too, but I don’t think three item tuples could be. Since lists and atoms are literal I think there could be ambiguity between AST nodes and values. Something like {:x, [], []} might be representing x() or it might be the literal value {:x, [], []}.

So I think it could be either “all tuples are literals, except three item tuples”, or it could be “only two element tuples are literals”. I think having a single case where the rule doesn’t apply is more confusing than having a single case where the rule does apply. Though it could be that there is no ambiguity with three item tuples and I’m just overlooking something.

1 Like

I actually really really really hate that things like 2-element tuples and literals are special cased, they lose their source positions and meta information. I wish everything, absolutely everything were 3-tuples.

Thankfully you can wrap literals in blocks to recover meta information, but you cannot do that to 2-tuples as things break I found out.

1 Like

Thanks for taking over the posting responsibility for these videos. I’ve been really busy lately.

1 Like

You’re welcome! I think the discussion is very valuable :slight_smile:

Thanks for the feedback everyone, I really appreciate it!

Great to see you again at the conf - thanks for the kind words! The trick I’ve found is to trip up a lot myself, and then just try to document everything that didn’t make any sense to me. :stuck_out_tongue:

For sure, I’d love to have had another 30 minutes to talk so I could go deeper! :grin: I personally don’t have a ton of programming history prior to Elixir, so how macros work and especially the AST in general were difficult concepts for me to learn. I’ve heard some excellent talks that started just a couple levels above where my own knowledge was, so at the time I didn’t learn much because I couldn’t follow along.

So my top goal for this talk was that someone with very little background would walk out of the talk understanding 90% of what was covered, and that they could then watch any other Macro talks and be able to easily follow along by recognizing the same concepts. I also went long every time I did a run-through of the talk so I ended up cutting a couple of examples at the end that would’ve introduced a few more of concepts of Macros (e.g. I really wanted to do an example of __using__ but just didn’t have the time).

As some others said, the reason for the 2-element tuple is to allow for the syntax of keyword lists, which I think might be important for the maintaining of do/else/etc blocks inside of the AST. I was planning to cover this in my original talk but had to cut it for time at the last minute. I’ve got the slide to prove it! There’s some additional info (though not a ton) in the Official Docs: Syntax Reference.

Also, could you add the elixirconf2017 tag to this post, thanks!


Thanks for the additional info! Also I’ve tagged this with elixirconf2017, didn’t realize that tag existed, will check the other posts as well.

Seems like a worthy goal to me and I would say that you nailed it.


/Side note/

You can use a hash to auto-link to a tags page when in a post, e.g.: #elixirconf2017 :023:


This was my first real foray into elixir metaprogramming and it was exactly what I needed. It gave me a general understanding of how it works, and the spark to dig deeper to really be able to use it. Thanks @jessejanderson

1 Like