This is the mail archive of the
kawa@sourceware.org
mailing list for the Kawa project.
Re: comparing char primitives
On Feb 16, 2012, at 7:59 PM, Per Bothner wrote:
On 02/16/2012 04:05 PM, Jamison Hope wrote:
Hello,
I'm looking to see if there's a way to compare char primitives
directly, as in
boolean javaCharEq(char c1, char c2) { return c1 == c2; }
...
Am I correct in concluding that currently there is no way to compare
unboxed chars? It looks like that's also the case for boolean.
The following seems to work:
(define (eqc (c1::char) (c2::char))
(let ((i1 ::int c1) (i2 ::int c2))
(= i1 i2)))
Good idea, I hadn't tried casting to ints. Your version does a couple
extra loads and stores (since the ints are bound to variables), but
it put me on the right track to test
(define (eqc (c1 ::char) (c2 ::char)) ::boolean
(= (as int c1) (as int c2)))
(define (eqb (b1 ::boolean) (b2 ::boolean)) ::boolean
(= (as int b1) (as int b2)))
which both compile to
0: iload_0
1: iload_1
2: if_icmpne 9
5: iconst_1
6: goto 10
9: iconst_0
10: ireturn
which looks pretty optimal to me (and happens to be exactly the
same as the bytecode for my javaCharEq test).
Optimizing char=? etc might be a reasonable thing to do, and
not that difficult.
Cool, I'll play around with it a bit as I continue to look at
srfi13.scm.
Complicating things is a bit is there is no primitive type
corresponding to (32-bit or at least 20-bit) Unicode character
- we have to use int. At least not in Java. No reason we couldn't
add one in Kawa. I.e. we could define a "character" type that is
implemented as int, but boxes to gnu.text.Char. A similar mechanism
could be used to implement signed number types, e.g. "signed-int".
Or more generally any final class with a single immutable field.
It's something I've been warning to do - but haven't had time for.
An extension of LangPrimType might be a good start, though that
has some issues with conversions.
Interesting idea. Maybe later..
--
Jamison Hope
The PTR Group
www.theptrgroup.com