www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Operator implicit conversion difference

reply ixid <adamsibson hotmail.com> writes:
This may have been overlooked in my other thread so I wanted to 
ask again:

This seems very inconsistent, does a += b not lower to a = a + b? 
I guess not based on the below:

     ushort a = ushort.max, b = ushort.max;


     a += b; // Compiles fine
     a = a + b; // Error: cannot implicitly convert expression 
(cast(int)a + cast(int)b) of type int to ushort
Nov 05 2015
next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 11/05/2015 05:20 AM, ixid wrote:

 This seems very inconsistent, does a += b not lower to a = a + b?
Apparently not: http://dlang.org/expression.html#AssignExpression It says "The right operand is implicitly converted to the type of the left operand". So, the rules are different. Ali
Nov 05 2015
prev sibling parent reply BBaz <bb.temp gmx.com> writes:
On Thursday, 5 November 2015 at 13:20:26 UTC, ixid wrote:
 This may have been overlooked in my other thread so I wanted to 
 ask again:

 This seems very inconsistent, does a += b not lower to a = a + 
 b? I guess not based on the below:

     ushort a = ushort.max, b = ushort.max;


     a += b; // Compiles fine
     a = a + b; // Error: cannot implicitly convert expression 
 (cast(int)a + cast(int)b) of type int to ushort
What's inconsistent is the integral promotion of the add expression result that stops from 4 bytes int: --- int a, b; a += b; a = a + b; --- is compiled but according to the specs, a + b result should be widened to long: http://dlang.org/expression.html#AddExpression (ubyte, byte) until (uint int) should be widened and (long , ulong) wrapped. This behavior would match the specs better.
Nov 06 2015
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 11/06/2015 04:56 PM, BBaz wrote:
 On Thursday, 5 November 2015 at 13:20:26 UTC, ixid wrote:
 This may have been overlooked in my other thread so I wanted to ask
 again:

 This seems very inconsistent, does a += b not lower to a = a + b? I
 guess not based on the below:

     ushort a = ushort.max, b = ushort.max;


     a += b; // Compiles fine
     a = a + b; // Error: cannot implicitly convert expression
 (cast(int)a + cast(int)b) of type int to ushort
What's inconsistent is the integral promotion of the add expression result that stops from 4 bytes int: --- int a, b; a += b; a = a + b; --- is compiled but according to the specs, a + b result should be widened to long: http://dlang.org/expression.html#AddExpression (ubyte, byte) until (uint int) should be widened and (long , ulong) wrapped. This behavior would match the specs better.
You say 'long' but according to integer promotions, a and b should both be promoted to 'int' and the result of 'a + b' is int: http://dlang.org/type.html#integer-promotions Ali
Nov 06 2015
parent BBaz <bb.temp gmx.com> writes:
On Saturday, 7 November 2015 at 01:10:01 UTC, Ali Çehreli wrote:
 On 11/06/2015 04:56 PM, BBaz wrote:
 On Thursday, 5 November 2015 at 13:20:26 UTC, ixid wrote:
 [...]
What's inconsistent is the integral promotion of the add expression result that stops from 4 bytes int: --- int a, b; a += b; a = a + b; --- is compiled but according to the specs, a + b result should be widened to long: http://dlang.org/expression.html#AddExpression (ubyte, byte) until (uint int) should be widened and (long , ulong) wrapped. This behavior would match the specs better.
You say 'long' but according to integer promotions, a and b should both be promoted to 'int' and the result of 'a + b' is int: http://dlang.org/type.html#integer-promotions Ali
oh...sorry I thought that the widening was done to the follwing type that's bigger, eg byte->short, short->int. So no inconsistence at all.
Nov 06 2015