What does this clojure.core.typed type error mean?

I want to get a type check validation clojure.core.typed, but I get a type error that I don't understand.

My questions:

  • What does the error below mean?
  • How can i fix this?

Here is my code (which I understand is incorrect):

(ns clj.util.map
  (:require [clojure.core.typed :as t]))

(t/ann map-vals
       (All [k v1 v2]
            (Fn [ (Fn [(t/Option v1) -> (t/Option v2)])
                  (t/Option (t/Map k v1)) ->
                  (t/Option (t/Map k v2)) ])))

(defn map-vals
  ;; FIXME: Incorrect code
  "Apply a function to each of the values in a map, returning the updated map."
  [f hm]
  (t/doseq> [k :- Any (keys hm)]
            (assoc hm k (f (get hm k)))))

Here is the result lein typed check clj.util.map:

Initializing core.typed ...
"Elapsed time: 6697.604 msecs"
core.typed initialized.
Start collecting clj.util.map
Finished collecting clj.util.map
Collected 1 namespaces in 6851.111 msecs
Start checking clj.util.map
Checked clj.util.map in 968.041 msecs
Checked 1 namespaces (approx. 21 lines) in 7823.552 msecs
Type Error (clj.util.map:14:23) Polymorphic function clojure.core/keys could not be applied to arguments:
Polymorphic Variables:
    k

Domains:
    (t/Map k Any)

Arguments:
    (t/Option (t/Map k v1))

Ranges:
    (t/Seq k) :object {:path [Keys], :id 0}

in: (clojure.core/keys hm)
in: (clojure.core/seq (clojure.core/keys hm))


Type Checker: Found 1 error
Found errors
Subprocess failed
+4
source share
1 answer

The first part of the error says that this refers to the call clojure.core/keys. You can find the type with (cf keys).

(All [k] [(Map k Any) -> (Seq k) :object {:id 0 :path [Keys]}])

The error basically summarizes this polymorphic type, mapped to the actual types provided by the function.

Polymorphic Variables All . k - , , k.

Domains ( ->). arities Fn, .

Arguments , . 14:23 (keys hm), : (t/Option (t/Map k v1))

Ranges ( ->) .

Expected, Range , Arguments Domain.

, Domains Arguments. Arguments Domains, . no Domains Arguments, ​​ , ; , , .

(t/Option (t/Map k v1)) (t/Map k Any), . , (t/Option (t/Map k v1)) (U nil (t/Map k v1)), nil (t/Map k Any).

. , keys nil. :

(keys (or hm {}))

+13
source

Source: https://habr.com/ru/post/1526197/


All Articles