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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Roman Liutikov
December 07, 2019
Programming
0
45
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
230
React & ClojureScript in production at Attendify
roman01la
0
200
Web Apps performance & JavaScript compilers
roman01la
3
120
Introduction to React.js
roman01la
0
84
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
400
React Native: Native mobile development with JavaScript
roman01la
0
190
ClojureScript, что ты такое?
roman01la
1
210
ClojureScript: what are you?
roman01la
2
140
Other Decks in Programming
See All in Programming
最初からAWS CDKで技術検証してもいいんじゃない?
akihisaikeda
4
130
モジュラモノリスにおける境界をGoのinternalパッケージで守る
magavel
0
3.5k
ふつうのRubyist、ちいさなデバイス、大きな一年 / Ordinary Rubyists, Tiny Devices, Big Year
chobishiba
1
430
Goの型安全性で実現する複数プロダクトの権限管理
ishikawa_pro
1
200
コーディングルールの鮮度を保ちたい / keep-fresh-go-internal-conventions
handlename
0
190
守る「だけ」の優しいEMを抜けて、 事業とチームを両方見る視点を身につけた話
maroon8021
3
750
2026年は Rust 置き換えが流行る! / 20260220-niigata-5min-tech
girigiribauer
0
230
20260228_JAWS_Beginner_Kansai
takuyay0ne
5
480
米国のサイバーセキュリティタイムラインと見る Goの暗号パッケージの進化
tomtwinkle
2
550
DevinとClaude Code、SREの現場で使い倒してみた件
karia
1
1k
Go Conference mini in Sendai 2026 : Goに新機能を提案し実装されるまでのフロー徹底解説
yamatoya
0
560
Agentic AI: Evolution oder Revolution
mobilelarson
PRO
0
150
Featured
See All Featured
Git: the NoSQL Database
bkeepers
PRO
432
66k
Fireside Chat
paigeccino
42
3.8k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.7k
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
630
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
0
220
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
210
Into the Great Unknown - MozCon
thekraken
40
2.3k
Building AI with AI
inesmontani
PRO
1
790
The Illustrated Children's Guide to Kubernetes
chrisshort
51
52k
Code Reviewing Like a Champion
maltzj
528
40k
A better future with KSS
kneath
240
18k
Paper Plane
katiecoart
PRO
0
48k
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