Running the following script results in a reflection warning that I would like to get rid of:

(ns reflection-test)

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

(deftype foo
  [#^ints bar])

(defn clone-foo-bar
  [#^::foo x]
  (aclone (:bar x)))

(clone-foo-bar (foo (int-array [1 2 3])))


The warning is:
Reflection warning, /Users/hinsen/test/reflection_test.clj:10 - call to aclone can't be resolved.

In principle the compiler has all the information to figure out that (:bar x) is an integer array, but apparently it doesn't. The only workaround I found is to declare the function in a protocol and implement that protocol right in the deftype:

(ns reflection-test-2)

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

(defprotocol foo-protocol
  (clone-foo-bar [x]))

(deftype foo
  [#^ints bar]
  foo-protocol
    (clone-foo-bar []
      (aclone bar)))

(clone-foo-bar (foo (int-array [1 2 3])))


However, this is limited to functions that take a ::foo object as their first argument. Is there a way to avoid reflection without that restriction?

Konrad.

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