I have encountered a problem when using deftype in Clojure. If I run the following code:
(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)
Then the compiler throws: ClassCastException MyProject.core.TestType cannot be cast to MyProject.core.TestType. Am I doing something wrong, or is this a bug? Note that if I remove the type annotation from test-function, so it's just:
(defn test-function [a-testtype]
(print (.geta a-testtype)))
Then the code works fine, but I get a warning (with warn-on-reflect enabled) about reflection, and it runs slower, which defeats the purpose of using deftype in my current use-case.
Edit: Okay, the code works in the repl, but not when I load it using ctrl-alt-s (I'm running it in Eclipse via Counterclockwise). So the problem seems to be with Eclipse or Counterclockwise.
This kind of thing happens when you redefine a type (with
deftype
ordefrecord
) but somewhere there's a use of the previously existing class hanging around, in your case in the type hint.I couldn't reproduce the behavior you describe with CountercClockwise's CtrlAltS, but it does appear evaluating the following expressions in a fresh REPL, so it might somehow help to diagnose your specific situation.