The nature of truth

Per Bothner per@bothner.com
Sun Jul 20 00:11:00 GMT 2003


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/




More information about the Kawa mailing list