Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ClojureScript × Type Inference

ClojureScript × Type Inference

Roman Liutikov

December 07, 2019
Tweet

More Decks by Roman Liutikov

Other Decks in Programming

Transcript

  1. 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?
  2. (if ^boolean (yes) (no)) if ( ) { yes(); }

    else { no(); } “type hint”
  3. Analyzer WARNING: cljs.core/+, all arguments must be numbers, got [number

    string] instead (defn weirdo [a b] (str a b)) (+ 1 (weirdo 2 3))
  4. Return Type Inference (defn ^string str [a b] ...) (defn

    weirdo [a b] (str a b)) (+ 1 (weirdo 2 3))
  5. Externs Inference (defn http-get [url] (js/fetch url)) (-> (apply http-get

    "example.com") (.then on-ok) (.catch on-error))
  6. Externs Inference (defn http-get [url] (js/fetch url)) (-> ^js/Promise (apply

    http-get "example.com") (.then on-ok) (.catch on-error))
  7. Specializations (def x "string") (def y #js [1 2 3])

    (empty? x) (empty? y) x.length === 0 y.length === 0
  8. Higher-order inference (defn f [] ([] 1) ([x] (str x

    "y"))) (apply f xs) #{number string} (apply f "x" xs) 'string
  9. Type Inference + clojure.spec (s/fdef component :args (s/cat :x map?

    :y string?)) (defui component [x y] [:h1 x (str "Hello, " y)])
  10. 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