Skip to content

Instantly share code, notes, and snippets.

@echosa
Created August 30, 2014 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save echosa/06c51ee6b985464220fe to your computer and use it in GitHub Desktop.
Save echosa/06c51ee6b985464220fe to your computer and use it in GitHub Desktop.
Clojure core.typed comparator
;; Trying to annotate a comparator that sorts a list of namespaces alphabetically
zork-fortress.core=> (clojure.core.typed/cf (comp (t/fn [ns1 :- clojure.lang.Namespace ns2 :- clojure.lang.Namespace] :- t/Num (compare (str ns1) (str ns2)))))
Type Error (/private/var/folders/s1/t0k7x43122ggbclq88zgjl400000gn/T/form-init8319721319789341749.clj:1:24) Polymorphic function comp could not be applied to arguments:
Polymorphic Variables:
x
y
b
Domains:
(t/IFn [x -> y]) (t/IFn [b ... b -> x])
Arguments:
(t/IFn [clojure.lang.Namespace clojure.lang.Namespace -> java.lang.Number])
Ranges:
(t/IFn [b ... b -> y])
in: (comp (do :clojure.core.typed.special-form/special-form :clojure.core.typed/fn {:ann (quote ({:ret-type {:type t/Num}, :dom ({:type clojure.lang.Namespace} {:type clojure.lang.Namespace})}))} (fn* ([ns1 ns2] (clojure.lang.Util/compare (str ns1) (str ns2))))))
ExceptionInfo Type Checker: Found 1 error clojure.core/ex-info (core.clj:4403)
@krisajenkins
Copy link

At first glance, using comp is odd. comp is for composing functions - I think you want (partial sort (fn ...)).

@krisajenkins
Copy link

But sort's going to want something that's actually an instance of java.util.Comparator.

Try this:

(ann compare-ns
  (t/I java.util.Comparator
       (t/IFn [clojure.lang.Namespace clojure.lang.Namespace -> t/Num])))

(def compare-ns
  (comparator str))

@echosa
Copy link
Author

echosa commented Aug 30, 2014

I thought (comp) was (comparator). Didn't know that latter existed. Simply switching from the former to the latter makes it validate. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment