unsigned policy

Sean Kelly sean at f4.ca
Wed Feb 14 13:19:31 PST 2007


Andrei Alexandrescu (See Website For Email) wrote:
> Derek Parnell wrote:
>> On Wed, 14 Feb 2007 12:24:59 -0800, Andrei Alexandrescu (See Website For
>> Email) wrote:
>>
>>
>>> x = y;    // should not work, use x = cast(bool)y or x = y ? 1 : 0
>>
>> Did you mean ...
>>   int x;
>>   bool y;
>>   x = y;    // should not work, use x = cast(int)y or x = y ? 1 : 0
>>
>> I'm in full agreement with your bool/int rules, BTW.
> 
> Yah, that's correct, thanks for the fix. Let's hear what Walter has to 
> say :o). In fact, I suggest that we all think of some compelling cases 
> one way or another. We all know of the mess created by implicit 
> bool->int in C++, so let's look for some "positive" example. On both sides.

The only one I can think of is using bool as a primitive constrained 
type.  For example, BitArray can be initialized using an array of bools as:

     BitArray b;
     b.init( [1,0,1,0] );

With the conversion rules in place, this would have to be rewritten as:

     BitArray b;
     b.init( [true,false,true,false] );

Not too pretty :-)  But bool is a logical type so it's really being 
misused here anyway.  What we really want is:

     alias ${0,1} bit;
     class BitArray { void init( bit[] buf ) {} }

Or something like that.  I've personally never had much of a need to 
convert bool->int and vice-versa.  In the few instances where this has 
been necessary, it's easy enough to use:

     int  i;
     bool b;
     b = i != 0;
     i = b ? 1 : 0;


Sean



More information about the Digitalmars-d mailing list