Skip to content

Commit f057e3c

Browse files
mfikesdnolen
authored and
dnolen
committed
CLJS-1582: Type-hint extend-type first arg for primitives
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.
1 parent c3899ac commit f057e3c

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Diff for: src/main/clojure/cljs/core.cljc

+31
Original file line numberDiff line numberDiff line change
@@ -1470,6 +1470,34 @@
14701470
(recur (conj seen fname) (next methods)))))
14711471
(recur (conj protos proto) impls)))))
14721472

1473+
(core/defn- type-hint-first-arg
1474+
[type-sym argv]
1475+
(assoc argv 0 (vary-meta (argv 0) assoc :tag type-sym)))
1476+
1477+
(core/defn- type-hint-single-arity-sig
1478+
[type-sym sig]
1479+
(list* (first sig) (type-hint-first-arg type-sym (second sig)) (nnext sig)))
1480+
1481+
(core/defn- type-hint-multi-arity-sig
1482+
[type-sym sig]
1483+
(list* (type-hint-first-arg type-sym (first sig)) (next sig)))
1484+
1485+
(core/defn- type-hint-multi-arity-sigs
1486+
[type-sym sigs]
1487+
(list* (first sigs) (map (partial type-hint-multi-arity-sig type-sym) (rest sigs))))
1488+
1489+
(core/defn- type-hint-sigs
1490+
[type-sym sig]
1491+
(if (vector? (second sig))
1492+
(type-hint-single-arity-sig type-sym sig)
1493+
(type-hint-multi-arity-sigs type-sym sig)))
1494+
1495+
(core/defn- type-hint-impl-map
1496+
[type-sym impl-map]
1497+
(reduce-kv (core/fn [m proto sigs]
1498+
(assoc m proto (map (partial type-hint-sigs type-sym) sigs)))
1499+
{} impl-map))
1500+
14731501
(core/defmacro extend-type
14741502
"Extend a type to a series of protocols. Useful when you are
14751503
supplying the definitions explicitly inline. Propagates the
@@ -1500,6 +1528,9 @@
15001528
_ (validate-impls env impls)
15011529
resolve (partial resolve-var env)
15021530
impl-map (->impl-map impls)
1531+
impl-map (if ('#{boolean number} type-sym)
1532+
(type-hint-impl-map type-sym impl-map)
1533+
impl-map)
15031534
[type assign-impls] (core/if-let [type (base-type type-sym)]
15041535
[type base-assign-impls]
15051536
[(resolve type-sym) proto-assign-impls])]

0 commit comments

Comments
 (0)