From b803b3d7b922bcd68fec598be910350a4a13eb11 Mon Sep 17 00:00:00 2001 From: dnolen Date: Mon, 1 Jun 2015 14:04:19 -0400 Subject: [PATCH] CLJS-1200: compare behaves differently from Clojure drop type check from cljs.core/compare and move into IComparable implementations. fast path in cljs.core/compare for numbers --- src/main/cljs/cljs/core.cljs | 39 +++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/src/main/cljs/cljs/core.cljs b/src/main/cljs/cljs/core.cljs index 13d47c93dd..8bcdb650dd 100644 --- a/src/main/cljs/cljs/core.cljs +++ b/src/main/cljs/cljs/core.cljs @@ -1103,7 +1103,9 @@ IComparable (-compare [this other] - (garray/defaultCompare (.valueOf this) (.valueOf other)))) + (if (instance? js/Date other) + (garray/defaultCompare (.valueOf this) (.valueOf other)) + (throw (js/Error. (str "Cannot compare " this " to " other)))))) (extend-type number IEquiv @@ -1935,13 +1937,18 @@ reduces them without incurring seq initialization" (nil? y) 1 - (identical? (type x) (type y)) - (if (implements? IComparable x) - (-compare ^not-native x y) - (garray/defaultCompare x y)) + (number? x) (if (number? y) + (garray/defaultCompare x y) + (throw (js/Error. (str "Cannot compare " x " to " y)))) + + (satisfies? IComparable x) + (-compare x y) :else - (throw (js/Error. "compare on non-nil objects of different types")))) + (if (and (or (string? x) (array? x) (true? x) (false? x)) + (identical? (type x) (type y))) + (garray/defaultCompare x y) + (throw (js/Error. (str "Cannot compare " x " to " y)))))) (defn ^:private compare-indexed "Compare indexed collection." @@ -8783,16 +8790,28 @@ reduces them without incurring seq initialization" ;; IComparable (extend-protocol IComparable Symbol - (-compare [x y] (compare-symbols x y)) + (-compare [x y] + (if (symbol? y) + (compare-symbols x y) + (throw (js/Error. (str "Cannot compare " x " to " y))))) Keyword - (-compare [x y] (compare-keywords x y)) + (-compare [x y] + (if (keyword? y) + (compare-keywords x y) + (throw (js/Error. (str "Cannot compare " x " to " y))))) Subvec - (-compare [x y] (compare-indexed x y)) + (-compare [x y] + (if (vector? y) + (compare-indexed x y) + (throw (js/Error. (str "Cannot compare " x " to " y))))) PersistentVector - (-compare [x y] (compare-indexed x y))) + (-compare [x y] + (if (vector? y) + (compare-indexed x y) + (throw (js/Error. (str "Cannot compare " x " to " y)))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Reference Types ;;;;;;;;;;;;;;;;