Possible to avoid reflection in this deftype in 1.3 alpha1?

9 views
Skip to first unread message

Andy Fingerhut

unread,
Sep 27, 2010, 4:58:58 PM9/27/10
to clo...@googlegroups.com
The following program compiles and runs perfectly fine in both 1.2 and
1.3 alpha1. It has no reflection warnings in 1.2, but it does in 1.3
alpha1. I have tried several variations, but I haven't yet been able
to figure out how to write type declarations that avoid reflection in
1.3 alpha1. Does anyone know whether this is a bug?

Or perhaps code like this ought to be structured completely
differently in order to avoid reflection, and it was just a fluke that
it worked in 1.2?

Thanks,
Andy

(ns nbody
(:gen-class))

(set! *warn-on-reflection* true)

(definterface IBody
(^double x [])
(^double y [])
(^double z [])
(^double dist [other]))

(deftype Body [^{:unsynchronized-mutable true :tag double} x
^{:unsynchronized-mutable true :tag double} y
^{:unsynchronized-mutable true :tag double} z ]
IBody
(x [this] x)
(y [this] y)
(z [this] z)
(dist [this other]
(let [^Body nbody other
dx (- x (.x nbody)) ; first reflection warning here
dy (- y (.y nbody)) ; second here
dz (- z (.z nbody)) ; third here
dsq (+ (* dx dx)
(+ (* dy dy)
(* dz dz)))]
(Math/sqrt dsq))))

(defn -main [& args]
(let [b (Body. 0 0 0)]
(println "pos:" (.x b) (.y b) (.z b))))

Stuart Halloway

unread,
Sep 27, 2010, 7:14:26 PM9/27/10
to clo...@googlegroups.com
That's weird. I see no reflection warnings when loading this on 1.3 alpha 1.

Stu

> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

Andy Fingerhut

unread,
Sep 27, 2010, 8:15:13 PM9/27/10
to clo...@googlegroups.com
OK, so I happened to stumble across a fix to my program, but I still
wonder whether this is intentional behavior in 1.3 alpha1 or not.

If I change the name 'nbody' in these lines:

> (let [^Body nbody other
> dx (- x (.x nbody)) ; first reflection warning here
> dy (- y (.y nbody)) ; second here
> dz (- z (.z nbody)) ; third here

to some name that is not the name of the namespace, like 'nbo', then
the reflection warnings go away:

> (let [^Body nbo other
> dx (- x (.x nbo)) ; first reflection warning here
> dy (- y (.y nbo)) ; second here
> dz (- z (.z nbo)) ; third here


If I then change the ns declaration at the top to "ns nbo", the
reflection warnings come back again.

Easy to avoid the problem, once you know.

Andy

Andy Fingerhut

unread,
Sep 27, 2010, 8:16:11 PM9/27/10
to clo...@googlegroups.com
I don't know if it makes a difference, but I was doing AOT compilation
on a file nbody.clj containing the program in my message.

I replied to my own message with a workaround that it seems to be
related to the variable 'nbody' having the same name as the one in the
'ns' declaration.

Thanks,
Andy

Reply all
Reply to author
Forward
0 new messages