|
| 1 | +# Metaprogamming Elixir by Chris McCord |
| 2 | + |
| 3 | +_Write less code, get more done (and have fun!)_ :purple_heart: |
| 4 | + |
| 5 | +## Abstract |
| 6 | + |
| 7 | +Reading this book to learn about how macros work and following some of the relevant |
| 8 | +examples. This README will serve as my notes, therefore you shouldn't take them at face |
| 9 | +value as the notes will make sense for me as I cherry pick a sentence or an analogy. |
| 10 | +Moreover, don't expect direct quotes, changing the sentences vocbulary helps me personally. |
| 11 | + |
| 12 | +## Chapter 1 - The language of macros |
| 13 | + |
| 14 | +- Macros are code that write code. |
| 15 | +- Elixir itself is made with macros, as a result you can extend the language itself |
| 16 | + to include things you think you might need. |
| 17 | +- Metaprogramming in elixir serves the purpose of extensibility by design. |
| 18 | +- With this power one can even define languages within elixir. The following is a valid Elixir program. |
| 19 | + |
| 20 | +```elixir |
| 21 | +div do |
| 22 | + h1 class: "title" do |
| 23 | + text "Hello" |
| 24 | + end |
| 25 | + p do |
| 26 | + text "Metaprogramming Elixir" |
| 27 | + end |
| 28 | +end |
| 29 | +"<div><h1 class=\"title\">Hello</h1><p>Metaprogramming Elixir</p></div>" |
| 30 | +``` |
| 31 | + |
| 32 | +#### The Abstract Syntax Tree |
| 33 | + |
| 34 | +- Most languages use AST but you never need to know about them. They are used typically during compilation |
| 35 | + or interpretation to transform source code into a tree structure before being turned into bytecode |
| 36 | + or machine code.. |
| 37 | +- José Valim, the creator of Elixir, chose to expose this AST and the syntax to interact with it. |
| 38 | +- We can now operate at the same level as the compiler. |
| 39 | +- Metaprogramming in Elixir revolves around manipulating and accessing ASTs. |
| 40 | +- To access the AST representation we use the `quote` macro. |
| 41 | + |
| 42 | +```elixir |
| 43 | +iex> quote do: 1 + 2 |
| 44 | +{:+, [context: Elixir, import: Kernel], [1, 2]} |
| 45 | +``` |
| 46 | + |
| 47 | +```elixir |
| 48 | +iex> quote do: div(10, 2) |
| 49 | +{:div, [context: Elixir, import: Kernel], [10, 2]} |
| 50 | +``` |
| 51 | + |
| 52 | +- This is the internals of the Elixir language itself. |
| 53 | +- This gives you easy options for infering meaning and optimising performance all while being within Elixirs high level syntax. |
| 54 | +- The purpose of macros is to interact with this AST with the syntax of Elixir. |
| 55 | +- Macros turn you from language consumer to language creator. You have the same level of power as José when he wrote the standard library. |
| 56 | + |
| 57 | +#### Trying It All Together |
| 58 | + |
| 59 | +"Let's write a macro that can print the spoken form of an Elixir mathematical expression, such as 5 + 2, when calculating a result. |
| 60 | +In most languages, we would have to parse a string expression into something digestible by our program. With Elixr, we can access |
| 61 | +the representation of expressions directly with macros." |
| 62 | + |
| 63 | +[First macro](math.exs) |
0 commit comments