+ operators

Steven Schveighoffer schveiguy at yahoo.com
Sat Jun 11 18:54:49 PDT 2011


On Sat, 11 Jun 2011 18:32:59 -0400, Jonathan M Davis <jmdavisProg at gmx.com>  
wrote:

> On 2011-06-11 14:54, Renoir wrote:
>> Sorry for the question but i'm an absolutely noob
>> I have:
>>
>> byte x = 10;
>> byte y = 3;
>> x = x + y;
>>
>> why compilers complains?
>>
>> Error: cannot implicitly convert expression (cast(int)x + cast(int)
>> y
>> ) of type int to byte
>>
>> Have i to make another cast to sum byte + byte?
>
> All integral operations where the types are int or smaller result in an  
> int
> (unless you're dealing with unsigned types, in which case, I believe  
> that the
> result would be uint). So, in this case the result of x + y is int. So,  
> if you
> want the result to be byte, you have to do
>
> x = cast(byte)(x + y);

If I'm not mistaken, the original code should be handled (and compile  
without errors) by range propagation.  Is that not fully implemented?

-steve


More information about the Digitalmars-d-learn mailing list