【发布时间】:2014-01-21 09:44:07
【问题描述】:
我在 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)
然后编译器抛出:ClassCastException MyProject.core.TestType cannot be cast to MyProject.core.TestType。我做错了什么,还是这是一个错误?请注意,如果我从测试函数中删除类型注释,那么它只是:
(defn test-function [a-testtype]
(print (.geta a-testtype)))
然后代码工作正常,但我收到关于反射的警告(启用了warn-on-reflect),并且运行速度较慢,这违背了在我当前的用例中使用 deftype 的目的。
编辑:好的,代码在 repl 中工作,但当我使用 ctrl-alt-s 加载它时不起作用(我在 Eclipse 中通过逆时针运行它)。所以问题似乎出在 Eclipse 或逆时针。
【问题讨论】:
-
在我这边无法重现。 Clojure 1.5.1
-
我也是。不可重现。 Clojure 1.5.1
-
我也在运行 1.5.1,但是在 Eclipse 中,通过逆时针,加载了 core.typed。我刚刚在 repl 中尝试过,它在那里工作,只是当我用 ctrl-alt-s 加载代码时就不行了。
标签: java clojure jvm clojure-java-interop counterclockwise