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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Felipe Renan
November 29, 2018
Programming
0
44
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
45
Git
feliperenan
3
61
Other Decks in Programming
See All in Programming
QAフローを最適化し、品質水準を満たしながらリリースまでの期間を最短化する #RSGT2026
shibayu36
2
4.3k
カスタマーサクセス業務を変革したヘルススコアの実現と学び
_hummer0724
0
650
AIと一緒にレガシーに向き合ってみた
nyafunta9858
0
170
CSC307 Lecture 08
javiergs
PRO
0
670
Architectural Extensions
denyspoltorak
0
280
Basic Architectures
denyspoltorak
0
660
2026年 エンジニアリング自己学習法
yumechi
0
130
コマンドとリード間の連携に対する脅威分析フレームワーク
pandayumi
1
450
組織で育むオブザーバビリティ
ryota_hnk
0
170
360° Signals in Angular: Signal Forms with SignalStore & Resources @ngLondon 01/2026
manfredsteyer
PRO
0
120
Implementation Patterns
denyspoltorak
0
280
LLM Observabilityによる 対話型音声AIアプリケーションの安定運用
gekko0114
2
420
Featured
See All Featured
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
130
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
160
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
300
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
62
49k
How To Stay Up To Date on Web Technology
chriscoyier
791
250k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
2
930
Unsuck your backbone
ammeep
671
58k
A Tale of Four Properties
chriscoyier
162
24k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
90
Six Lessons from altMBA
skipperchong
29
4.1k
Un-Boring Meetings
codingconduct
0
200
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 :)