[Gambas-user] Referencing an array element with boolean

Patrik Karlsson pata.karlsson at ...626...
Sun Jun 22 07:48:54 CEST 2014


2014-06-21 12:28 GMT+02:00 Tobias Boege <taboege at ...626...>:

> On Sat, 21 Jun 2014, Patrik Karlsson wrote:
> > 1. Why is Abs(True) True?
>
> Because the mapping Integer -> Boolean is not injective. The rule is: 0
> maps
> to False, everything else maps to True. To be able to map Boolean ->
> Integer
> the convention used in Gambas is False maps to zero (of course) and True
> maps
> to -1, which makes actually sense if you don't look into the array index
> but
> into the bit flags direction (True means "all bits set" in Gambas, which is
> what -1 is in two's complement). So what happens is:
>
> Abs(True) becomes Abs(CInt(True)) on the fly which is Abs(-1) = 1 which is
> non-zero and thus True again.


 That was interesting, thanks.

/Patrik



More information about the User mailing list