Discussion:
Strange behavior with a mapping-function
m***@flow.net
2015-10-22 21:28:47 UTC
Permalink
Hey Ambrose,

The following is giving me trouble:

(defalias Pair '[String String])

(ann get-second-fails [(t/Vec Pair) -> (t/Seqable String)])
(defn get-second-fails [in]
(map second in))

(ann get-second-works [(t/Vec Pair) -> (t/Seqable String)])
(defn get-second-works [in]
(map (t/fn [i :- Pair] (second i)) in))

In a nutshell, passing the second function directly to map doesn't work - I
have to wrap it in the core.typed function macro.

What's up with that?

Cheers.

(error is:

Type Error (rest_api/types.clj:459:3) Polymorphic function map could not be
applied to arguments:
Polymorphic Variables:
c
a
b

Domains:
[a b ... b -> c] (t/NonEmptySeqable a) (t/NonEmptySeqable b) ... b
[a b ... b -> c] (t/U (Seqable a) nil) (t/U (Seqable b) nil) ... b

Arguments:
(t/All [x] (t/IFn [(t/HSequential [t/Any x t/Any *]) -> x :object {:id 0,
:path [(Nth 1)]}] [(t/Option (t/I (t/CountRange 0 1) (Seqable x))) -> nil]
[(t/I (t/CountRange 2) (Seqable x)) -> x] [(t/Option (Seqable x)) ->
(t/Option x)])) (IPersistentVector Pair)

Ranges:
(t/NonEmptyASeq c)
(t/ASeq c)

with expected type:
(t/Seqable String)


in: (map second in)
Ambrose Bonnaire-Sergeant
2015-10-23 01:52:20 UTC
Permalink
Hi Matthew,

Polymorphic arguments to polymorphic functions must be instantiated. See
this
<https://frenchy64.github.io/typed/clojure,/core.typed,/clojure/2013/09/03/polymorphic-hof.html>
blog.

Something like (map (inst second String) in) should work.

Thanks,
Ambrose
Post by m***@flow.net
Hey Ambrose,
(defalias Pair '[String String])
(ann get-second-fails [(t/Vec Pair) -> (t/Seqable String)])
(defn get-second-fails [in]
(map second in))
(ann get-second-works [(t/Vec Pair) -> (t/Seqable String)])
(defn get-second-works [in]
(map (t/fn [i :- Pair] (second i)) in))
In a nutshell, passing the second function directly to map doesn't work -
I have to wrap it in the core.typed function macro.
What's up with that?
Cheers.
Type Error (rest_api/types.clj:459:3) Polymorphic function map could not
c
a
b
[a b ... b -> c] (t/NonEmptySeqable a) (t/NonEmptySeqable b) ... b
[a b ... b -> c] (t/U (Seqable a) nil) (t/U (Seqable b) nil) ... b
(t/All [x] (t/IFn [(t/HSequential [t/Any x t/Any *]) -> x :object {:id 0,
:path [(Nth 1)]}] [(t/Option (t/I (t/CountRange 0 1) (Seqable x))) -> nil]
[(t/I (t/CountRange 2) (Seqable x)) -> x] [(t/Option (Seqable x)) ->
(t/Option x)])) (IPersistentVector Pair)
(t/NonEmptyASeq c)
(t/ASeq c)
(t/Seqable String)
in: (map second in)
Continue reading on narkive:
Loading...