Slide 1

Slide 1 text

"Elixir took the Erlang virtual machine, BEAM, and put a sensible face on it. It gives you all the power of Erlang plus a powerful macro system." — Dave Thomas, author of Programming Elixir, co-author of Pragmatic Programmer

Slide 2

Slide 2 text

"Erlang plus a powerful macro system."

Slide 3

Slide 3 text

Elixir - Macros On Erlang

Slide 4

Slide 4 text

Who?! Thorsten Ball Software Developer thorstenball.com mrnugget / @thorstenball I like programming languages.

Slide 5

Slide 5 text

I wrote a book! Get it: interpreterbook.com Coupon code for 20% off: elixirfromthebembel

Slide 6

Slide 6 text

interpreterbook.com/lost

Slide 7

Slide 7 text

Disclaimer! Use in production at your own risk

Slide 8

Slide 8 text

What's a macro?

Slide 9

Slide 9 text

Macros — Code that writes code — They allow us to influence which code gets evaluated

Slide 10

Slide 10 text

So, meta-programming? — Short answer: It depends — Long answer: Iiiiitt deeeepeeeeeends — Best answer: Eeeh, maybe, depends on who you ask — Not the Ruby kind of meta-programming — Macros really work with code: not with objects/ classes/methods/functions/etc.

Slide 11

Slide 11 text

Two Camps Of Macro Systems — Text-Substitution Macros — Syntactic Macros

Slide 12

Slide 12 text

Two Camps Of Macro Systems — Search and Replace Macros — Code as Data Macros

Slide 13

Slide 13 text

Text-Substitution Macros — The simplest type of macro system — Example: The C Pre Processor (cpp)

Slide 14

Slide 14 text

#define GREETING "Hello there" int main(int argc, char *argv[]) { #ifdef DEBUG printf(GREETING " Debug-Mode!\n"); #else printf(GREETING " Production-Mode!\n"); #endif return 0; }

Slide 15

Slide 15 text

Text-Substitution Macros — You can go a long way with it — But you have to be careful: escaping, scoping, overwriting, ... — Much more like a templating system than a macro system of the second kind

Slide 16

Slide 16 text

Syntactic Macros — In the land where "Code Is Data" — Yeah, really, it is weird. It needs a small push to wrap one's head around it

Slide 17

Slide 17 text

Code is data?

Slide 18

Slide 18 text

Code starts out as text — We write code in our text editor — We look at text diffs on GitHub — We store text in our version control system — We modify source code with search/replace

Slide 19

Slide 19 text

But then we pass it to our programming language... ... and you won't believe what happens next.

Slide 20

Slide 20 text

Parsers turn code into data! — Parsing: turning strings into data structures — Just like a JSON parser turns a string into objects, arrays, integers, strings, ... — Parsers turn code strings into syntax trees (or Abstract Syntax Trees)

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

Why? — Analyze it — "is this valid syntax?" — "has this identifier been defined before?" — "is this code unused?" — Modify it — "Let's remove the commented-out code" — "Let's replace calls to this function with the body of the function itself" — Pass it around — "lets hand this and the code from the other files to the pretty printer"

Slide 23

Slide 23 text

Pretty handy, huh?

Slide 24

Slide 24 text

In language where "code is data" you can do that in the language itself!

Slide 25

Slide 25 text

Allow me to explain... — Ruby — Implemented in C — Parse and modify Ruby in C — Parser in C, AST in C, code that uses AST in C — Elixir — "Code is data" — Implemented in Erlang (doesn't matter) — You can parse and modify Elixir in Elixir — Parse in Elixir, AST in Elixir, code that uses AST in Elixir

Slide 26

Slide 26 text

Code is data, data is code — Incredibly powerful — Writing code that writes code — The language becomes self-aware...

Slide 27

Slide 27 text

Recursion, baby!

Slide 28

Slide 28 text

(! Lisp (baby))

Slide 29

Slide 29 text

Code is data — Sounds weird? It is — Sounds abstract? It is — Don't get it? That's okay

Slide 30

Slide 30 text

Let's write a macro in Elixir The unless macro. The example for macros - in any language! Should look like this: unless 5 == 3, do: IO.inspect("will be printed") unless 3 == 3, do: IO.inspect("will not be printed")

Slide 31

Slide 31 text

Unless - The Naiive Version defmodule FunctionUnless do def unless(clause, do: block) do if !clause, do: block end end

Slide 32

Slide 32 text

Looking good there... iex(1)> c "unless.exs" [FunctionUnless] iex(2)> FunctionUnless.unless 5 == 3, do: IO.puts("yay condition false") yay condition false [do: :ok]

Slide 33

Slide 33 text

Uh oh iex(3)> FunctionUnless.unless 5 == 5, do: IO.puts("yay condition true!") yay condition true! nil

Slide 34

Slide 34 text

Macros to the rescue defmodule MacroUnless do defmacro unless(clause, block) do quote do if !unquote(clause), do: unquote(block) end end end

Slide 35

Slide 35 text

Just what the alchemist ordered iex(1)> c "unless.exs" [FunctionUnless, MacroUnless] iex(2)> require MacroUnless MacroUnless iex(3)> MacroUnless.unless 5 == 3, do: IO.puts("condition false") condition false :ok iex(4)> MacroUnless.unless 5 == 5, do: IO.puts("condition true") nil

Slide 36

Slide 36 text

How? — Macros are evaluated in the "Macro Expansion Phase" — Macro Expansion: replace calls to macros in the source code with the result of the call — Parsing -> Macro Expansion Phase -> Compilation/ Interpretation — Macros receive AST nodes and return AST nodes

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

Inspecting a macro defmodule MacroUnless do defmacro unless(clause, do: block) do IO.inspect(clause) IO.inspect(block) quote do if !unquote(clause), do: unquote(block) end end end

Slide 39

Slide 39 text

Inspecting a macro iex(9)> MacroUnless.unless 5 == 5, do: IO.puts("condition true") {:==, [line: 9], [5, 5]} {{:., [line: 9], [{:__aliases__, [counter: 0, line: 9], [:IO]}, :puts]}, [line: 9], ["condition true"]} nil Tuples, keyword lists, lists - code is data!

Slide 40

Slide 40 text

Return the code you want to see in the world! — quote — unquote

Slide 41

Slide 41 text

quote iex> quote do: 1 + 2 {:+, [context: Elixir, import: Kernel], [1, 2]} iex> quote do: IO.puts("foobar") {{:., [], [{:__aliases__, [alias: false], [:IO]}, :puts]}, [], ["foobar"]} iex> quote do: MyModule.add_two(2) {{:., [], [{:__aliases__, [alias: false], [:MyModule]}, :add_two]}, [], [2]} iex> quote do: [1, 2, 3, 4] [1, 2, 3, 4] iex> quote do: [head | tail] = [1, 2, 3, 4] {:=, [], [[{:|, [], [{:head, [], Elixir}, {:tail, [], Elixir}]}], [1, 2, 3, 4]]}

Slide 42

Slide 42 text

unquote — The trusty helper of quote — Punches a hole into quote

Slide 43

Slide 43 text

unquote iex> quote do: 1 + 2 + 3 {:+, [context: Elixir, import: Kernel], [{:+, [context: Elixir, import: Kernel], [1, 2]}, 3]} iex> quote do: 1 + unquote(2 + 3) {:+, [context: Elixir, import: Kernel], [1, 5]}

Slide 44

Slide 44 text

We need unquote iex(18)> quote do: if !(clause), do: block {:if, [context: Elixir, import: Kernel], [{:!, [context: Elixir, import: Kernel], [{:clause, [], Elixir}]}, [do: {:block, [], Elixir}]]}

Slide 45

Slide 45 text

Unquote to access arguments iex(19)> clause = quote do: 5 == 5 {:==, [context: Elixir, import: Kernel], [5, 5]} iex(20)> block = quote do: IO.puts("yay!") {{:., [], [{:__aliases__, [alias: false], [:IO]}, :puts]}, [], ["yay!"]} iex(21)> quote do: if !(unquote(clause)), do: unquote(block) {:if, [context: Elixir, import: Kernel], [{:!, [context: Elixir, import: Kernel], [{:==, [context: Elixir, import: Kernel], [5, 5]}]}, [do: {{:., [], [{:__aliases__, [alias: false], [:IO]}, :puts]}, [], ["yay!"]}]]}

Slide 46

Slide 46 text

Our macro again defmodule MacroUnless do defmacro unless(clause, block) do quote do if !unquote(clause), do: unquote(block) end end end

Slide 47

Slide 47 text

Boom! Macros! Code is data!

Slide 48

Slide 48 text

Elixir - Macros On Top Of Erlang — Why "Erlang" and "a macro system"? — Because it's an incredibly powerful macro system — Elixir itself is built using this macro system

Slide 49

Slide 49 text

Macros let you cross the boundary between language creator and language user

Slide 50

Slide 50 text

* Use sparingly

Slide 51

Slide 51 text

Thank you

Slide 52

Slide 52 text

Writing An Interpreter In Go Coupon code for 20% off: elixirfromthebembel