Hi all,

(There is an old thread on a similar topic to this, but I seem to be
unable to reply to the group about it, only to the author).

I've been using Clojure this last week, and in general enjoying it
very much. There have been a small number of jarring moments though,
to do with Java interop, so I thought I'd check in with my questions.

The main one comes from having a (third-party) Java class that has
methods a bit like this:

class C {
    public void floatMethod(float f) { /* ... */ }
    public void floatArrayMethod (float[] argv, int flags) { /* ...
*/ }
}

The argument unboxing that Clojure does with method calls means that
this worked fine:

(.floatMethod (C.) (float 1))

So, lulled into a false sense of knowing what was going on, I tried
this:

(.floatArraymethod (C.) (into-array [(float 1) (float 2)]) 0)

..and got a ClassCastException. The right method gets found,
presumably just by arity, but the array that gets passed has type
java.lang.Float[] (which I would have known had I RTFM properly).

It turns out that the unboxing doesn't extend to array types. Which I
can understand as a design decision, though it would certainly be nice
and sugary otherwise. However, it leaves me doing things like this:

(def floatarray (make-array Float/TYPE 2))
(for [i (range (alength floatarray))] (aset floatarray i (float ([1 2]
i))))

Aside from the misuse of 'for' (which then needs to be iterated to
cause the side-effects), what am I missing here? Hints on idiomatic
Clojure as well as my primitives question are most welcome.

J

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

Reply via email to