Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Elixir metaprogramming
Search
Felipe Renan
November 29, 2018
Programming
0
43
Elixir metaprogramming
Felipe Renan
November 29, 2018
Tweet
Share
More Decks by Felipe Renan
See All by Felipe Renan
Elixir - Tic Tac Toe
feliperenan
0
44
Git
feliperenan
3
60
Other Decks in Programming
See All in Programming
MCPでVibe Working。そして、結局はContext Eng(略)/ Working with Vibe on MCP And Context Eng
rkaga
5
2.3k
はじめてのMaterial3 Expressive
ym223
2
740
AWS発のAIエディタKiroを使ってみた
iriikeita
1
190
go test -json そして testing.T.Attr / Kyoto.go #63
utgwkk
3
300
意外と簡単!?フロントエンドでパスキー認証を実現する WebAuthn
teamlab
PRO
2
760
機能追加とリーダー業務の類似性
rinchoku
2
1.3k
Updates on MLS on Ruby (and maybe more)
sylph01
1
180
print("Hello, World")
eddie
2
530
実用的なGOCACHEPROG実装をするために / golang.tokyo #40
mazrean
1
280
Vue・React マルチプロダクト開発を支える Vite
andpad
0
110
The Past, Present, and Future of Enterprise Java with ASF in the Middle
ivargrimstad
0
120
CloudflareのChat Agent Starter Kitで簡単!AIチャットボット構築
syumai
2
500
Featured
See All Featured
What's in a price? How to price your products and services
michaelherold
246
12k
Thoughts on Productivity
jonyablonski
70
4.8k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
34
6k
How GitHub (no longer) Works
holman
315
140k
Why Our Code Smells
bkeepers
PRO
339
57k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
8
920
Code Reviewing Like a Champion
maltzj
525
40k
Visualization
eitanlees
148
16k
Being A Developer After 40
akosma
90
590k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
50k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
48
9.7k
Transcript
Carregando…
Felipe Renan feliperenan @feeliperenan
None
Elixir Metaprogramming
O que é metaprogramação?
“Metaprogramação é a programação de programas que escrevem ou manipulam
outros programas…”
https://pt.wikipedia.org/ “Metaprogramação é a programação de programas que escrevem ou
manipulam outros programas…”
Metaprogramação é código que gera código.
Elixir é escrito em Elixir.
Abstract Syntax Tree (AST) & Macros
Abstract Syntax Tree (AST) Para quem não é familiar com
AST, muitas linguagens de programação tem uma AST. Quando o nosso programa é compilado ou interpretado, o código que escrevemos é transformado em uma arvore antes de ser tornar bytecode ou machine code.
1 + 2 * 3
+ 1 * 2 3 Esse processo geralmente fica escondido
da gente e geralmente nunca precisamos pensar sobre isso. Porém, em Elixir, esse processo fica exposto para o desenvolvedor.
+ 1 * 2, 3
+ 1 * 2, 3 Func Func
+ 1 * 2, 3 Args Args
+(1, *(2, 3))
{func, metadata, args}
iex> quote do: 2 * 3 {:*, _, [2, 3]}
iex> quote do: 2 * 3 {:*, _, [2, 3]}
ARGS Func
iex> quote do: 1 + 2 * 3 {:+, ...,
[1, {:*, ..., [2, 3]}]}
{:+, ..., [1, {:*, ..., [2, 3]}]}
{:+ 1, {:* 2, 3}} {:+, ..., [1, {:*, ...,
[2, 3]}]}
{:+ 1, {:* 2, 3}} +(1, *(2, 3)) {:+, ...,
[1, {:*, ..., [2, 3]}]}
Um pouco mais sobre quote…
iex> name = "Felipe Renan" "Felipe Renan"
iex> name = "Felipe Renan" "Felipe Renan" iex> "My name
is name" "My name is name”
iex> name = "Felipe Renan" "Felipe Renan" iex> "My name
is name" "My name is name” iex> "My name is #{name}” "My name is Felipe Renan”
iex> quote do: 2 * 3 {:*, _, [2, 3]}
iex> num = 1 1 iex> quote do: 2 *
3 {:*, _, [2, 3]}
iex> num = 1 1 iex> quote do: num {:num,
[], Elixir} iex> quote do: 2 * 3 {:*, _, [2, 3]}
iex> num = 1 1 iex> quote do: unquote(num) 1
iex> quote do: num {:num, [], Elixir} iex> quote do: 2 * 3 {:*, _, [2, 3]}
iex> num = 1 1
iex> quote do: 1 + num {:+, …, [1, {:num,
[], Elixir}]} iex> num = 1 1
iex> quote do: 1 + unquote(num) {:+, …, [1, 1]}
iex> quote do: 1 + num {:+, …, [1, {:num, [], Elixir}]} iex> num = 1 1
Macros
iex> require Math iex> Math.say 1 + 2 1 +
2 ------ 3
None
{:+, _, args}
{:+, _, [2, 3]}
None
None
iex> require Math iex> Math.say 1 + 2 1 +
2 ------ 3
Quase tudo são macros in Elixir.
def & defmodule
If & unless
|>
Macros Expansion
ControlFlow.unless 1 == 1, do: "Entrou no bloco!!!” => nil
ControlFlow.unless 1 == 1, do: "Entrou no bloco!!!” => nil
ControlFlow.unless 1 != 1, do: "Entrou no bloco!!!” => “Entrou no bloco!!!”
None
None
None
ControlFlow.unless 1 == 1, do: "Entrou no bloco!!!”
ControlFlow.unless 1 == 1, do: "Entrou no bloco!!!” if !1
== 1, do: "Entrou no bloco!!!"
ControlFlow.unless 1 == 1, do: "Entrou no bloco!!!” if !1
== 1, do: "Entrou no bloco!!!" case !(1 == 1) do x when x in [false, nil] -> nil _ -> "block entered" end
Kernel.SpecialForms As macros definidas aqui são blocos fundamentais no Elixir
que não podem ser sobre-escritas. Elas também representam o fim da estrada das expansões das macros.
Macro rules
* Não escreva macros
* Não escreva macros * Mas aprenda como elas funcionam
Referências • https://pragprog.com/book/cmelixir/metaprogramming-elixir • Elixir in Elixir by Jay Hayes
• Don’t write macros But do learn how they work - Jesse Anderson
Obrigado :)