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
0
43
ClojureScript × Type Inference
Roman Liutikov
December 07, 2019
Tweet
Share
More Decks by Roman Liutikov
See All by Roman Liutikov
React Kyiv – Dec 19, 2017
roman01la
1
220
React & ClojureScript in production at Attendify
roman01la
0
200
Web Apps performance & JavaScript compilers
roman01la
3
120
Introduction to React.js
roman01la
0
78
React Native: Native Mobile Development in JavaScript @ LvivJS 2016
roman01la
0
140
React Native: Are we there yet? (Pokémon edition) @ VinnytsiaJS '16
roman01la
0
380
React Native: Native mobile development with JavaScript
roman01la
0
180
ClojureScript, что ты такое?
roman01la
1
210
ClojureScript: what are you?
roman01la
2
130
Other Decks in Programming
See All in Programming
After go func(): Goroutines Through a Beginner’s Eye
97vaibhav
0
350
コードとあなたと私の距離 / The Distance Between Code, You, and I
hiro_y
0
110
Serena MCPのすすめ
wadakatu
4
960
非同期jobをtransaction内で 呼ぶなよ!絶対に呼ぶなよ!
alstrocrack
0
690
登壇は dynamic! な営みである / speech is dynamic
da1chi
0
280
Devvox Belgium - Agentic AI Patterns
kdubois
1
110
バッチ処理を「状態の記録」から「事実の記録」へ
panda728
PRO
0
140
NixOS + Kubernetesで構築する自宅サーバーのすべて
ichi_h3
0
530
どの様にAIエージェントと 協業すべきだったのか?
takefumiyoshii
2
640
止められない医療アプリ、そっと Swift 6 へ
medley
1
150
CSC509 Lecture 03
javiergs
PRO
0
330
CSC305 Lecture 03
javiergs
PRO
0
240
Featured
See All Featured
The Illustrated Children's Guide to Kubernetes
chrisshort
48
51k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
15
1.7k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
114
20k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
33
2.5k
The Cult of Friendly URLs
andyhume
79
6.6k
How to train your dragon (web standard)
notwaldorf
96
6.3k
Why You Should Never Use an ORM
jnunemaker
PRO
59
9.6k
Producing Creativity
orderedlist
PRO
347
40k
Building Applications with DynamoDB
mza
96
6.7k
Code Reviewing Like a Champion
maltzj
525
40k
Six Lessons from altMBA
skipperchong
28
4k
KATA
mclloyd
32
15k
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