On Jul 17, 2012, at 4:19 , Theam Yong Chew wrote:
Hi folks,
I was wondering how we can conveniently pass longs, bytes into normal Java functions/constructors, for example,
(jnew "java.lang.Byte" 23) ==> Java exception 'java.lang.NoSuchMethodException: Byte(java.lang.Integer)'. [Condition of type JAVA-EXCEPTION]
Here only constructor available is for "byte" inputs. It's a pain to have to use this every time we use a byte, long, or similar:
(jnew (jconstructor "java.lang.Byte" "byte") 23) #<java.lang.Byte 23 {3A9300BF}>
Relatedly/alternatively, is there a way convenient to coerce values to a certain type, forcing 23 above (default int/Integer) to be a byte or long?
I haven't thought about this in any detail, but my gut feeling is that in general you want to avoid automagic typecasting. Otherwise, calling constructors overloaded with, e.g., byte vs integer becomes an upredictable mess. I guess both of your use cases above could be served with something like
(defun jbyte (number) (jnew (jconstructor "java.lang.Byte" "byte") number))
plus some more magic if the value passed in is already a java.lang.Byte, or other number-like object (downcasting a Java Integer object to Byte if within range, etc.) Similar for the other primitive Java datatypes. (I don't know if we have something like that already, but it does seem useful.)
Rudi