2013-08-01 87 views
1

我尋求信息類型的提示用Clojure的範圍,例如,如果我寫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] ...) 

回答

2

只需設置警告反射爲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) 

簡短的回答你的每一個問題是沒有:(