Why there is too many uneccessary casts?

Steven Schveighoffer schveiguy at yahoo.com
Tue Jun 11 19:15:08 PDT 2013


On Tue, 11 Jun 2013 20:58:17 -0400, Ali Çehreli <acehreli at yahoo.com> wrote:

> On 06/11/2013 05:48 PM, captaindet wrote:
>
>  > i think part of the problem is that '1' is an int. so the calculation
>  > must be promoted to integer.

No, the compiler knows that 1 could fit in a ubyte.  It has  
value-range-propagation.  Changing 1 to a ubyte type would not fix the  
problem.

The issue is the integer promotion when doing arithmetic.

> According to "Integer Promotions" and "Usual Arithmetic Conversions"  
> there is no arithmetic operation that is executed in any type narrower  
> than int:
>
>    http://dlang.org/type.html

This is exactly right, and the rule is:

"4. Else the integer promotions are done on each operand..."

which means both are upgraded to int before anything else occurs.

-Steve


More information about the Digitalmars-d-learn mailing list