Elixir metaprogramming
-
Upload
kamil-lelonek -
Category
Software
-
view
775 -
download
5
Transcript of Elixir metaprogramming
Metaprogramming in ElixirA brief introduction
What will be about?
macros quote and unquote DSLs a.k.a. __using__
Rules regarding macrosMACRO RULE #1
DON'T WRITE MACROS
MACRO RULE #2
USE MACROS GRATUITOUSLY
What are macros?A code that writes code
Where are macros?
Elixir itself is primarily built with macros
ifunless
cond
def defmodule
assert
How do macros work?
https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/kernel.ex
Macro expanding
What can macros do?
Why to use macros?
Extending the language to your needs
Boilerplate removal
ASTs
quote
iex(1)> quote do: 1 + 2 {:+, [context: Elixir, import: Kernel], [1, 2]}
Always an atom or another tupleMetadata
The arguments for the function call
unquote
iex(2)> Macro.to_string { ...(2)> :div, ...(2)> [context: Elixir, import: Kernel], ...(2)> [10, 2] ...(2)> } "div(10, 2)"
iex(1)> quote do: div(10, 2) { :div, [context: Elixir, import: Kernel], [10, 2] }
__using__
Custom type
DB migration
Model
Operations
KamilLelonek / exnumerator
Learning materials
http://bit.ly/elixir-metaprogramminghttp://bit.ly/elixir-macros
https://github.com/KamilLelonek/exnumerator
https://blog.lelonek.me/
http://woumedia.com/
https://twitter.com/KamilLelonek