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
ClojureScript × Type Inference
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Roman Liutikov
December 07, 2019
Programming
54
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
ClojureScript × Type Inference
Roman Liutikov
December 07, 2019
Transcript
37 slides
ClojureScript Type Inference
None
Type Inference a conclusion reached on the basis of evidence
and reasoning
Type Inference a conclusion reached on the basis of evidence
and reasoning 2019 is it a number? "2019" is it a number? can I add them?
Why in ClojureScript? Generate optimal JavaScript Catch stupid bugs at
compile time Improve externs inference
(if (yes) (no)) if (cljs.core.truth_( )) { yes(); } else
{ no(); }
(if ^boolean (yes) (no)) if ( ) { yes(); }
else { no(); } “type hint”
Types 'string 'number 'boolean 'seq 'array 'object 'any 'clj-nil nil
Types (defn f [x] (if x 1 "2")) #{number string}
Compiler phases Read Analyze Emit
Compiler phases Read Analyze Emit "(let [x 1] ...)"
Compiler phases Read Analyze Emit {:op :let :body ... :bindings
[{:name 'x :init {:op :const}} ...]}
Compiler phases Read Analyze Emit var x = 1;
Analyzer (defn weirdo [a b] (str a b)) (+ 1
(weirdo 2 3))
Analyzer 1 + weirdo(2, 3) WARNING: cljs.core/+, all arguments must
be numbers, got [number string] instead
Analyzer WARNING: cljs.core/+, all arguments must be numbers, got [number
string] instead (defn weirdo [a b] (str a b)) (+ 1 (weirdo 2 3))
Return Type Inference (defn ^string str [a b] ...) (defn
weirdo [a b] (str a b)) (+ 1 (weirdo 2 3))
String Coercion Elision (str a b) [str(a), str(b)].join("");
String Coercion Elision (str ^string a ^string b) [a, b].join("");
Predicate-Induced Inference (if (string? x) (str x "y") :else)
Specializations (def x "string") (def y #js [1 2 3])
(count x) (count y)
Specializations (def x "string") (def y #js [1 2 3])
x.length y.length
Externs Inference (defn http-get [url] (js/fetch url)) (-> (apply http-get
"example.com") (.then on-ok) (.catch on-error))
Externs Inference (defn http-get [url] (js/fetch url)) (-> ^js/Promise (apply
http-get "example.com") (.then on-ok) (.catch on-error))
Experiments
Dead Code Elimination (if x :then :else) 'clj-nil :else
Dead Code Elimination (if x :then :else) #{number string} :then
Specializations (def x "string") (def y #js [1 2 3])
(first x) (first y) x[0] y[0]
Specializations (def x "string") (def y #js [1 2 3])
(second x) (second y) x[1] y[1]
Specializations (def x "string") (def y #js [1 2 3])
(empty? x) (empty? y) x.length === 0 y.length === 0
Higher-order inference (identity 1) 'any (identity 1) 'number
Higher-order inference (apply str xs) 'any (apply str xs) 'string
Higher-order inference (defn f [] ([] 1) ([x] (str x
"y"))) (apply f xs) #{number string} (apply f "x" xs) 'string
Higher-order inference (map inc xs) ['seq 'number] (first xs) 'number
Type Inference + clojure.spec (defui component [x] [:h1 x]) attributes
map or child element?
Type Inference + clojure.spec (s/fdef component :args (s/cat :x map?
:y string?)) (defui component [x y] [:h1 x (str "Hello, " y)])
Summary Compiler gets smarter Your code gets faster without you
doing anything You don't have to know all of this But it might be useful to optimize hot path
More Decks by Roman Liutikov
See All by Roman Liutikov
React Kyiv – Dec 19, 2017
roman01la
1
240
React & ClojureScript in production at Attendify
roman01la
0
210
Web Apps performance & JavaScript compilers
roman01la
3
150
Introduction to React.js
roman01la
0
100
React Native: Native Mobile Development in JavaScript @ LvivJS 2016
roman01la
0
170
React Native: Are we there yet? (Pokémon edition) @ VinnytsiaJS '16
roman01la
0
450
React Native: Native mobile development with JavaScript
roman01la
0
210
ClojureScript, что ты такое?
roman01la
1
230
ClojureScript: what are you?
roman01la
2
150
Other Decks in Programming
See All in Programming
AI時代のIaC入門 〜なぜインフラをコードで管理したいのか?〜 / Introduction to IaC in the AI Era
akihisaikeda
6
1.5k
AI時代のFlutter品質設計 — アプリ品質を守るQuality Layers
omatty198
0
370
AI-Powered Domain-Driven Design
ziobrando
0
430
DroidKnights 2026 Android A2UI Jetpack Compose 기반의 안전한 AI UI 렌더링 - 박제창
itsmedreamwalker
1
220
우리의 Nitro Modules는 당신의 Turbo Modules보다 아름답다
l2hyunwoo
0
260
Growing PicoRuby's Ecosystem on ESP32
yuuu
2
100
AIが生成したコードは読むべきか?抽象化設計とコードの品質保証 / abstraction-design-and-quality
minodriven
16
9k
Claude Code ハーネスDeep Dive – 君はどこまで制御する? -
takateeen_knj
6
5.4k
AWS CDKを理解するために公式の定義を深掘って解説してみる / Understanding AWS CDK by Digging into Its Official Definition
akihisaikeda
3
300
S3 TablesのIcebergデータに、AIエージェントからアクセスしてみた / 20261016developersio2026joetsu-accessing-iceberg-in-s3tables-via-agent
kasacchiful
PRO
0
300
Kawaiicon: Kubernetes, it's no longer cool, but let's secure it anyway!
barnbarn
0
130
Railsのためのパスキー実践入門 〜マルチテナントSaaSでの意思決定〜
utakaha
4
3.9k
Featured
See All Featured
Site-Speed That Sticks
csswizardry
13
1.6k
The Limits of Empathy - UXLibs8
cassininazir
2
770
Rebuilding a faster, lazier Slack
samanthasiow
85
9.8k
WCS-LA-2024
lcolladotor
0
990
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
2
520
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
1k
Automating Front-end Workflow
addyosmani
1370
210k
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
2
3k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.7k
GitHub's CSS Performance
jonrohan
1033
470k
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
2
3.8k
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
610