ClassCastException MyType无法强制转换为MyType?

Log*_*ins 2 java jvm clojure clojure-java-interop counterclockwise

我在Clojure中使用deftype时遇到了问题.如果我运行以下代码:

(defprotocol TestProt
  (geta [this])
  (getb [this]))

(deftype TestType [a b]
  TestProt
  (geta [this] a)
  (getb [this] b))

(defn test-function [^TestType a-testtype]
  (print (.geta a-testtype))) 

(def test-tt (TestType. 1 1))

(test-function test-tt)
Run Code Online (Sandbox Code Playgroud)

然后编译器抛出:ClassCastException MyProject.core.TestType无法强制转换为MyProject.core.TestType.我做错了什么,或者这是一个错误?请注意,如果我从test-function中删除类型注释,那么它只是:

(defn test-function [a-testtype]
  (print (.geta a-testtype))) 
Run Code Online (Sandbox Code Playgroud)

然后代码工作正常,但我得到一个关于反射的警告(启用了反射警告),并且它运行得更慢,这违背了在我当前用例中使用deftype的目的.

编辑:好的,代码在repl中工作,但是当我使用ctrl-alt-s加载它时(我在Eclipse中通过逆时针运行它).所以问题似乎是Eclipse或逆时针.

jua*_*rro 5

当你重新定义一个类型(带有deftype或者defrecord)时会发生这种事情,但是在某个地方使用了先前存在的类,在你的情况下是类型提示.

我无法重现您使用CountercClockwise描述的行为CtrlAltS,但它确实会在新的REPL中评估以下表达式,因此它可能以某种方式帮助诊断您的具体情况.

(defprotocol TestProt
  (geta [this])
  (getb [this]))

(deftype TestType [a b]
  TestProt
  (geta [this] a)
  (getb [this] b))

(defn test-function [^TestType a-testtype]
  (print (.geta a-testtype)))

(def test-tt (TestType. 1 1))

(println :first (test-function test-tt))

;= :first 1

;; redefine the type...
(deftype TestType [a b]
  TestProt
  (geta [this] a)
  (getb [this] b))

;; ...and the test-tt var with the new version     
(def test-tt (TestType. 1 1))

(println :second (test-function test-tt))

;= ClassCastException user.TestType cannot be cast to user.TestType  user/test-function (NO_SOURCE_FILE:89) 
Run Code Online (Sandbox Code Playgroud)