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
ClojureScript × Type Inference
Search
Roman Liutikov
December 07, 2019
Programming
48
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
ClojureScript × Type Inference
Roman Liutikov
December 07, 2019
More Decks by Roman Liutikov
See All by Roman Liutikov
React Kyiv – Dec 19, 2017
roman01la
1
230
React & ClojureScript in production at Attendify
roman01la
0
200
Web Apps performance & JavaScript compilers
roman01la
3
140
Introduction to React.js
roman01la
0
94
React Native: Native Mobile Development in JavaScript @ LvivJS 2016
roman01la
0
160
React Native: Are we there yet? (Pokémon edition) @ VinnytsiaJS '16
roman01la
0
420
React Native: Native mobile development with JavaScript
roman01la
0
200
ClojureScript, что ты такое?
roman01la
1
220
ClojureScript: what are you?
roman01la
2
140
Other Decks in Programming
See All in Programming
20260722_microCMSで考える、AI時代のコンテンツ運用設計
yosh1
0
370
Built Our Own Background Agent at LayerX
layerx
PRO
9
5k
ITヒヤリハットを整理してみた ~ライフサイクルと原因から考える再発防止策~
koukimiura
1
140
Detecting Compromised CI with eBPF and Cilium Tetragon
lizrice
0
160
関東Kaggler会_NVIDIA_Nemotron_コンペ_振り返り
rick_ds
0
470
Japan Community Day at Kubecon + CloudNativeCon Japan 2026: Learning Container Privilege Control by Building My Own Low-Level Container Runtime
ternbusty
1
140
源内ハンズオン概要編
hideg
0
120
人間の目はかわらない、だからJPEGは30年もつ
yuzneri
12
18k
PHP初心者セッション2026 〜生成AIでは見えない裏側を知る:今だからLAMPを通して仕組みを学ぶ〜
kashioka
0
850
2年かけて Deno に DOMMatrix を実装した話 / How I implemented DOMMatrix in Deno over two years
petamoriken
0
210
アルゴリズムは何を圧縮しているのか ─ Haskell から育った「圧縮代数」というメンタルモデル
naoya
16
3.9k
継続モナドとリアクティブプログラミング
yukikurage
3
690
Featured
See All Featured
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
980
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
630
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
360
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
570
Music & Morning Musume
bryan
47
7.3k
Visualization
eitanlees
152
17k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.2k
Making Projects Easy
brettharned
120
6.7k
Git: the NoSQL Database
bkeepers
PRO
432
67k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
360
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
350
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.5k
Transcript
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