1

我正在寻找有关 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] ...)

?

4

1 回答 1

2

只需将warn-on-reflection设置为true 并使用无法解析类型的函数测试您的表达式。

回复:

(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) 

您的每个问题的简短回答是否定的:(

于 2013-08-01T23:34:33.750 回答