Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Elixir metaprogramming
Search
Felipe Renan
November 29, 2018
Programming
64
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Elixir metaprogramming
Felipe Renan
November 29, 2018
Transcript
62 slides
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 :)
More Decks by Felipe Renan
See All by Felipe Renan
Elixir - Tic Tac Toe
feliperenan
0
62
Git
feliperenan
3
67
Other Decks in Programming
See All in Programming
느린 네트워크와 저사양 디바이스 사용자를 위한 웹뷰 로딩 시간 개선기
kmkim
1
270
Claude Codeのおかげでやりたいことがやれるようになったんだよォー!
sms_tech
0
180
AppFunctionsの内部実装を紐解く
hiroaki404
0
110
TS 7.0 で baseUrl が使えなくなった今、パスエイリアスをどうするか
bicstone
2
800
TanStack Vue Query と Pinia Colada から学ぶ非同期状態の型設計
aoshi
0
700
How SRE Grew Out of Server-Side Work and Became the Team's Own / サーバサイドから始まったSREを チームに渡すまで
maaaato
0
170
React Native経験者が Flutterを30⽇間触って驚いたこと
3saito
0
210
社内AIエージェント「Askワンバン」における コストチューニングの取り組み
t0yohei
0
180
将来の標準フォーマッター?ktfmtを深堀る
t45k
0
240
Secure Your Coding Agent Like It’s Malware (v1.5) 🇬🇧 @Code.talks 2026
richargh
0
200
선언형 햅틱 라이브러리 진동을 소개합니다
l2hyunwoo
1
330
Growing PicoRuby's Ecosystem on ESP32
yuuu
2
150
Featured
See All Featured
How Software Deployment tools have changed in the past 20 years
geshan
3
35k
Prompt Engineering for Job Search
mfonobong
0
600
Designing for humans not robots
tammielis
254
26k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Navigating Weather and Climate Data
rabernat
0
600
Un-Boring Meetings
codingconduct
0
510
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
670
Crafting Experiences
bethany
1
450
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
380
Building Adaptive Systems
keathley
44
3.3k
Believing is Seeing
oripsolob
1
290
Technical Leadership for Architectural Decision Making
baasie
4
660