以下示例将导致运行时类转换异常。似乎无法将类型注解 ^long
添加到协议中,因为它将导致编译时错误或运行时错误。
(defprotocol my-protocol-1
(my-func-1 [this value]))
(deftype my-type-1 [x]
my-protocol-1
(my-func-1 [this value]
(* value 2)))
(def z1 (my-type-1. 123))
(println (my-func-1 z1 99)) ;; this works
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defprotocol my-protocol-2
(my-func-2 [this ^long value])) ;; only a type hit has been added
(deftype my-type-2 [x]
my-protocol-2
(my-func-2 [this value]
(* value 2)))
(def z2 (my-type-2. 123))
(println (my-func-2 z2 456)) ;; will cause exception
;; class user$eval180$fn__181$G__171__188 cannot be cast to class clojure.lang.IFn$OLO