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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Felipe Renan
November 29, 2018
Programming
55
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Elixir metaprogramming
Felipe Renan
November 29, 2018
More Decks by Felipe Renan
See All by Felipe Renan
Elixir - Tic Tac Toe
feliperenan
0
54
Git
feliperenan
3
65
Other Decks in Programming
See All in Programming
Signal Forms: Details & Live Coding @enterJS 2026 in Mannheim
manfredsteyer
PRO
0
140
Javaの型とAI時代に型が大事な理由 / java types and type in AI era
kishida
2
140
The ROI of Quarkus for Spring Boot Applications
hollycummins
0
120
Composerを使ったサプライチェーン攻撃の様子を眺めてみる #phpstudy
o0h
PRO
2
250
A2UI という光を覗いてみる
satohjohn
1
140
Oxcを導入して開発体験が向上した話
yug1224
4
310
AIで効率化できた業務・日常
ochtum
0
140
その問い、本当に正しいですか?AI時代のエンジニアに必要な哲学と認知科学 / ai-philosophy-cognitive-science
minodriven
9
5.1k
Oxlintのカスタムルールの現況
syumai
6
1.1k
TAKTでAI駆動開発の品質を設計する
j5ik2o
7
1.3k
タクシーアプリ『GO』の バックエンド開発のおける AI利活用と若者のすべて
pyama86
3
2k
Mujeres en SEO Summit 2026 - Greatest Disaster Hits en Web Performance
guaca
0
180
Featured
See All Featured
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
210
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
240
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
610
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.6k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2k
Google's AI Overviews - The New Search
badams
0
1k
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
The agentic SEO stack - context over prompts
schlessera
0
820
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.9k
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.5k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
420
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
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 :)