2

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 を MyProject.core.TestType にキャストできません。私は何か間違ったことをしていますか、それともこれはバグですか? テスト関数から型注釈を削除すると、次のようになることに注意してください。

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

その後、コードは正常に動作しますが、リフレクションに関する警告 (warn-on-reflect が有効になっている場合) が表示され、実行が遅くなり、現在のユースケースで deftype を使用する目的が無効になります。

編集:わかりました、コードはreplで機能しますが、ctrl-alt-sを使用してロードすると機能しません(反時計回りでEclipseで実行しています)。したがって、問題はEclipseまたは反時計回りにあるようです。

4

1 に答える 1

5

deftypeこの種のことは、型を ( orを使用して)再定義したときに発生します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) 
于 2014-01-21T12:11:48.910 に答える