This is the mail archive of the
kawa@sources.redhat.com
mailing list for the Kawa project.
Re: The nature of truth
- From: Per Bothner <per at bothner dot com>
- To: Chris Dean <Chris dot Dean at sokitomi dot com>
- Cc: Kawa List <kawa at sources dot redhat dot com>
- Date: Sat, 19 Jul 2003 00:21:07 -0700
- Subject: Re: The nature of truth
- References: <14386.1058581406@mercedsystems.com>
Chris Dean wrote:
I understand that internally, Kawa does the equivalent of
if (x == Boolean.FALSE)
...
The only alternative (for Scheme) would be:
x instanceof Boolean && ! ((Boolean) x).booleanValue()
However for pure Scheme
(if x ... ...)
is the same as:
(if (not (eq? x #f)) ... ...)
I.e Scheme assumes there is only a single false object.
But I have a problem when I'm
in Scheme and call a Java method which returns a java.lang.Boolean. In
that situation the (x == Boolean.FALSE) logic no longer works. Why?
because the Java method is returning new Boolean( "false" ) which is not
the same object as the "interned" Boolean.FALSE object.
Why is the Java method doing something that silly?
What happens if it does Boolean.valueOf("false") instead?
2. This is not a bug. Scheme booleans are not Java Booleans. If you
wish to convert from one type to another use a function like:
(define (java-boolean->boolean x)
(define-namespace bool "class:java.lang.Boolean")
(if (instance? x <java.lang.Boolean>)
(bool:boolean-value x)
(if x
#t
#f)))
Why not just:
(define (java-boolean->boolean x)
(define-namespace bool "class:java.lang.Boolean")
(if (instance? x <java.lang.Boolean>)
(bool:boolean-value x)
#f)))
Though note neither this nor your version are type-consistent, though
Kawa will fix that.
3. This is a not a bug, but as a new feature Kawa will introduce the
magic type <Boolean> which will automatically convert to and from
Scheme <boolean> and Java java.lang.Boolean. Similar to the magic
<String> type which converts Scheme and Java strings.
We could do that, but I'm not convinved it should be the default way to
convert Objects to boolean.
P.S. For those not following along, here's a simple example. All
return values should be 'false-value.
(if (make <java.lang.Boolean> "false") 'true-value 'false-value)
=> true-value
My opinion: "don't do that".
--
--Per Bothner
per@bothner.com http://per.bothner.com/