在CLJS中构建自定义比较函数时,我遇到了一些我认为很奇怪的行为
直接在REPL中使用 >(或类似的)函数对两个字符串进行比较时,我会收到一个 :invalid-arithmetic 警告,并返回 nil(与CLJ引发异常类似)。如果我这样做,它将返回一个布尔值而没有任何警告(CLJ引发异常)
CLJS
clj꞉user꞉> (> "A" "Z")
nil
; ------ WARNING - :invalid-arithmetic -------------------------------------------
; Resource: <eval>:1:1
cljs.core/>, all arguments must be numbers, got [string string] instead
--------------------------------------------------------------------------------
clj꞉user꞉> ((fn [x y] (> x y)) "A" "Z")
false
CLJ
user=> (> "A" "Z")
Execution error (ClassCastException) at user/eval3 (REPL:1).
class java.lang.String cannot be cast to class java.lang.Number (java.lang.String and java.lang.Number are in module java.base of loader 'bootstrap')
user=> ((fn [x y] (> x y)) "A" "Z")
Execution error (ClassCastException) at user/eval5$fn (REPL:1).
class java.lang.String cannot be cast to class java.lang.Number (java.lang.String and java.lang.Number are in module java.base of loader 'bootstrap')
有人能解释发生了什么吗?
谢谢,
Julian