For fun, I've been porting a very simple particle animation from Java to
Clojure. It was somewhat slow so to see how fast I could make it I decided
to try using mutation. I've defined a Particle type, and a PSlice type that
contains an array of objects and a length (number of non-nil objects in the
slice), but when I try to provide type hints for this slice type, I get the
error:
*ClassCastException MyProject.core.PSlice cannot be cast to *
*MyProject.core.PSlice *Is this a bug, or am I doing something wrong? My
code defining the two types is:
(definterface IPt
(^Double gx []) (^Double gy []) (^Double gz []) (^Double gvx []) (^Double
gvy []) (^Double gvz []) (^Double gR []) (^Double glife []) (^Boolean gis
[])
(sx [^Double v]) (sy [^Double v]) (sz [^Double v]) (svx [^Double v]) (svy
[^Double v]) (svz [^Double v]) (sR [^Double v]) (slife [^Double v])
(^Boolean sis [^Boolean v]))
(deftype Particle [^:unsynchronized-mutable ^Double x
^:unsynchronized-mutable ^Double y ^:unsynchronized-mutable ^Double z
^:unsynchronized-mutable ^Double vx
^:unsynchronized-mutable ^Double vy ^:unsynchronized-mutable ^Double vz
^:unsynchronized-mutable ^Double R
^:unsynchronized-mutable ^Double life ^:unsynchronized-mutable ^Boolean is]
IPt
(gx [_] x) (gy [_] y) (gz [_] z) (gvx [_] vx) (gvy [_] vy) (gvz [_] vz)
(gR [_] R) (glife [_] life) (gis [_] is)
(sx [this v] (set! x v)) (sy [this v] (set! y v)) (sz [this v] (set! z v))
(svx [this v] (set! vx v)) (svy [this v] (set! vy v)) (svz [this v] (set!
vz v))
(sR [this v] (set! R v)) (slife [this v] (set! life v)) (sis [this v]
(set! is v)))
(definterface ISlice
(^Long gLen [])
(sLen [^Long x])
(^Particle gPt [^Long n])
(sPt [^Long n ^Particle pt] ))
(deftype PSlice [^"[Ljava.lang.Object;" pts ^:unsynchronized-mutable ^Long
len]
ISlice
(gLen [_] len)
(sLen [this new-len] (set! len new-len))
(gPt [this n] (aget pts n))
(sPt [this n pt] (aset pts n pt)))
Then the error occurs if I define a PSlice, such as by:
(def tslice (PSlice. (make-array Object 100) 0))
and a function:
(defn test-fn [^PSlice slice]
(print (.gLen slice))
)
Then call (test-fn tslice).
Note that the function will work as intended if no type hints are provided,
but will run quite slowly due to reflection, defeating the point of using
mutation.
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.