Skip to content

Commit

Permalink
CLJS-1582: Type-hint extend-type first arg for primitives
Browse files Browse the repository at this point in the history
If extending boolean or number to a protocol, propagate type hint to
first arg of fns.

This is done by walking the code in the impl-map, and associng the
passed type-sym as the :tag meta for the first argument of all fns.
  • Loading branch information
mfikes authored and dnolen committed Mar 18, 2016
1 parent c3899ac commit f057e3c
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/main/clojure/cljs/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1470,6 +1470,34 @@
(recur (conj seen fname) (next methods)))))
(recur (conj protos proto) impls)))))

(core/defn- type-hint-first-arg
[type-sym argv]
(assoc argv 0 (vary-meta (argv 0) assoc :tag type-sym)))

(core/defn- type-hint-single-arity-sig
[type-sym sig]
(list* (first sig) (type-hint-first-arg type-sym (second sig)) (nnext sig)))

(core/defn- type-hint-multi-arity-sig
[type-sym sig]
(list* (type-hint-first-arg type-sym (first sig)) (next sig)))

(core/defn- type-hint-multi-arity-sigs
[type-sym sigs]
(list* (first sigs) (map (partial type-hint-multi-arity-sig type-sym) (rest sigs))))

(core/defn- type-hint-sigs
[type-sym sig]
(if (vector? (second sig))
(type-hint-single-arity-sig type-sym sig)
(type-hint-multi-arity-sigs type-sym sig)))

(core/defn- type-hint-impl-map
[type-sym impl-map]
(reduce-kv (core/fn [m proto sigs]
(assoc m proto (map (partial type-hint-sigs type-sym) sigs)))
{} impl-map))

(core/defmacro extend-type
"Extend a type to a series of protocols. Useful when you are
supplying the definitions explicitly inline. Propagates the
Expand Down Expand Up @@ -1500,6 +1528,9 @@
_ (validate-impls env impls)
resolve (partial resolve-var env)
impl-map (->impl-map impls)
impl-map (if ('#{boolean number} type-sym)
(type-hint-impl-map type-sym impl-map)
impl-map)
[type assign-impls] (core/if-let [type (base-type type-sym)]
[type base-assign-impls]
[(resolve type-sym) proto-assign-impls])]
Expand Down

0 comments on commit f057e3c

Please sign in to comment.