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
34
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
170
Web Apps performance & JavaScript compilers
roman01la
3
120
Introduction to React.js
roman01la
0
73
React Native: Native Mobile Development in JavaScript @ LvivJS 2016
roman01la
0
120
React Native: Are we there yet? (Pokémon edition) @ VinnytsiaJS '16
roman01la
0
320
React Native: Native mobile development with JavaScript
roman01la
0
170
ClojureScript, что ты такое?
roman01la
1
200
ClojureScript: what are you?
roman01la
2
120
Other Decks in Programming
See All in Programming
Code Reviews
bkuhlmann
4
890
Behind VS Code Extensions for JavaScript / TypeScript Linnting and Formatting
unvalley
5
1.1k
OpenAPIを中心に考えるAPI開発入門 / Introduction to API Development with a Focus on OpenAPI
seike460
PRO
2
170
SwiftUIで使いやすいToastの作り方 / How to build a Toast system which is easy to use in SwiftUI
lovee
3
150
Node.js v22 で変わること
yosuke_furukawa
PRO
10
3.7k
GitHub Actionsで泣かないためにやっておきたい設定 / Recommended GHA settings to avoid crying
pinkumohikan
3
550
Tailwind CSSを本気でカスタマイズする方法
fsubal
14
5.4k
AWS CDKコントリビュートTIPS / aws-cdk-contribution-tips
gotok365
3
300
『Railsオワコン』と言われる時代に、なぜブルーモ証券はRailsを選ぶのか
free_world21
1
300
敵対的ポイフル
futabato
0
120
From Spring Boot 2 to Spring Boot 3 with Java 21 and Jakarta EE
ivargrimstad
0
320
Elm 0.19.0 Changes
bkuhlmann
0
490
Featured
See All Featured
Intergalactic Javascript Robots from Outer Space
tanoku
266
26k
Stop Working from a Prison Cell
hatefulcrawdad
267
19k
Designing with Data
zakiwarfel
96
4.8k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
501
140k
ParisWeb 2013: Learning to Love: Crash Course in Emotional UX Design
dotmariusz
104
6.6k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
226
51k
Embracing the Ebb and Flow
colly
80
4.2k
Visualization
eitanlees
137
14k
Mobile First: as difficult as doing things right
swwweet
217
8.6k
Rebuilding a faster, lazier Slack
samanthasiow
74
8.2k
Art, The Web, and Tiny UX
lynnandtonic
290
19k
Unsuck your backbone
ammeep
663
57k
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