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


On Sep 27, 2010, at 1:58 PM, Andy Fingerhut wrote:

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))))


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to