On Thu, May 6, 2010 at 15:22, Stuart Halloway <[email protected]> wrote:
> Reflection is slow, there are less than ten of these, and the possible set
> ain't gonna change.
>
> I would write a function that looks them up from a map.

Yea, I considered that too.  The only reason I didn't was because of a
misguided desire not to hardcode things.

But, you're right. My solution isn't optimal. The set of classes which
box java primitives is not going to change.

(let [m {Byte      Byte/TYPE,       Short     Short/TYPE,
         Integer   Integer/TYPE,    Long      Long/TYPE,
         Boolean   Boolean/TYPE,    Character Character/TYPE,
         Float     Float/TYPE,      Double    Double/TYPE}]
  (defn unbox [class]
    (m class class)))

I've defined 'm' in a let containing the definition of unbox because
I'm assuming that if I'd inlined it at the point where I use m that it
would get constructed once for every function call, but I don't
actually know that:

  (defn unbox [class]
    ({Byte ...} class class))

Would Clojure be smart enough to recognize that {Byte ...} is a
compile-time constant expression and not rebuild it on every call to
unbox?

// Ben

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