当前,{{compare}}(对于任何非CLJS IComparable类型)只能接受基元(数字、布尔值、字符串、数组),并将它们转发到{{goog.array.defaultCompare}}(它只是返回a > b ? 1 : a < b ? -1 : 0)。
尽管如此,标准定义了任何具有{{valueOf}}的东西都可以比较
https://mdn.org.cn/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Relational_operators
因为{{valueOf}}应该返回这些原语之一。例如,这可以工作
(let [x0 #js{:x 1, :valueOf #(-> 1)}
x1 #js{:x 2, :valueOf #(-> 2)}
dc garr/defaultCompare
(list (dc x0 x1) (dc x1 x0) (dc x1 x1)))
我们应该允许这样做,即不是
:else
(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))))
做
(if (and (or (string? x) (array? x) (true? x) (false? x)))
(and (js-in "valueOf" x) (js-in "valueOf" y)))
(identical? (type x) (type y)))
(garr/defaultCompare x y)
(throw (js/Error. (str "Cannot compare " x " to " y))))
我们还应该给{{js-in}}一个布尔标签。