我正在寻找有关Clojure中类型提示范围的信息,例如,如果我写的话
(defn big-add [^BigInteger x y] (.add x y))
与
相同(defn big-add [^BigInteger x ^BigInteger y] (.add x y))
?假设我写了
(defn big-sum
([] BigInteger/ZERO)
([^BigInteger x] x)
([^BigInteger x & more] (.add x (apply big-sum more) )))
Clojure是否认为more
已满BigInteger
?假设我不想告诉它?我会做点什么吗
(defn foo [^BigInteger x & ^Long more] ...)
?
答案 0 :(得分:2)
只需将 warn-on-reflection 设置为true,然后使用无法解析类型的函数测试表达式。
REPL:
(set! *warn-on-reflection* true)
(defn testo [s]
(str s))
=> #'user/testo
(defn testo [s]
(.charAt s 1))
=> Reflection warning, NO_SOURCE_PATH:2:8 - call to charAt can't be resolved.
(defn testo [^java.lang.String s]
(.charAt s 1))
=> #'user/testo
(defn testo [^java.lang.String s s2]
(.charAt s2 1))
=> Reflection warning, NO_SOURCE_PATH:2:8 - call to charAt can't be resolved.
(defn testo [^java.lang.String s & more]
(.charAt (first more) 1))
=> Reflection warning, NO_SOURCE_PATH:2:8 - call to charAt can't be resolved.
最后
(defn testo [s & ^java.lang.String more]
(.charAt (first more) 1))
=> CompilerException java.lang.RuntimeException: & arg cannot have type hint, compiling:(NO_SOURCE_PATH:1:1)
对每个问题的简短回答都是:(