Hi, Am 24.06.2009 um 14:21 schrieb arasoft:
Is there a way to avoid having to use a function like this
(defn coerce-primitive-integer [value to-class]
(cond (= to-class java.lang.Byte) (byte value)
(= to-class java.lang.Short) (short value)
(= to-class java.lang.Integer) (int value)
(= to-class java.lang.Long) (long value))
)
in order to coerce a number to the class of another number?
This cannot be a function, because functions
cannot return primitive types. Their return
value will always be boxed.
So you need a macro to do this. Furthermore
you can use condp to declutter the whole thing
a little bit.
(defmacro coerce-primitive-integer
[value to-class]
`(condp = ~to-class
Byte (byte ~value)
Short (short ~value)
Integer (int ~value)
Long (long ~value)))
Please note, that value can be dereferenced
several times, because the branches are
mutually exclusive. So it is evaluated only
once.
Hope this helps.
Sincerely
Meikel
smime.p7s
Description: S/MIME cryptographic signature
