www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why does this not work?

reply "Shachar" <shachar shemesh.biz> writes:
void func( int c )
{
     ubyte a;

     a = cast(ubyte)c + cast(ubyte)c;
}

dmd v2.065 complains about:
test.d(5): Error: cannot implicitly convert expression 
(cast(int)cast(ubyte)c + cast(int)cast(ubyte)c) of type int to 
ubyte

As far as I can tell, adding two values of the same type should 
result in the same type.

Shachar
Sep 17 2014
next sibling parent reply "flamencofantasy" <flamencofantasy gmail.com> writes:
the result of ubyte + ubyte is int I believe.

Try;
void func( int c )
{
      ubyte a;

      a = cast(ubyte)(cast(ubyte)c + cast(ubyte)c);
}
Sep 17 2014
parent reply "Shachar" <shachar shemesh.biz> writes:
On Wednesday, 17 September 2014 at 13:03:05 UTC, flamencofantasy 
wrote:
 the result of ubyte + ubyte is int I believe.

 Try;
 void func( int c )
 {
      ubyte a;

      a = cast(ubyte)(cast(ubyte)c + cast(ubyte)c);
 }
From http://dlang.org/type, under Usual Arithmetic Conversions: 4. Else the integer promotions are done on each operand, followed by: 1. If both are the same type, no more conversions are done. So, as far as I can see, the specs disagree with you. Shachar
Sep 17 2014
next sibling parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Wed, 17 Sep 2014 13:20:13 +0000
Shachar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:

  From http://dlang.org/type, under Usual Arithmetic Conversions:
 4. Else the integer promotions are done on each operand, followed=20
 by:
      1. If both are the same type, no more conversions are done.
it's bug in specs, i believe, 'cause compiler promotes smaller types to int/uint.
Sep 17 2014
parent reply Shachar Shemesh <shachar weka.io> writes:
On 17/09/14 16:32, ketmar via Digitalmars-d-learn wrote:
 On Wed, 17 Sep 2014 13:20:13 +0000
 Shachar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
 wrote:

   From http://dlang.org/type, under Usual Arithmetic Conversions:
 4. Else the integer promotions are done on each operand, followed
 by:
       1. If both are the same type, no more conversions are done.
it's bug in specs, i believe, 'cause compiler promotes smaller types to int/uint.
I don't understand. Why is this behavior preferrable to the one outlined by the specs? Thanks, Shachar
Sep 17 2014
next sibling parent "flamencofantasy" <flamencofantasy gmail.com> writes:
Because of overflow.



On Wednesday, 17 September 2014 at 13:36:42 UTC, Shachar Shemesh 
wrote:
 On 17/09/14 16:32, ketmar via Digitalmars-d-learn wrote:
 On Wed, 17 Sep 2014 13:20:13 +0000
 Shachar via Digitalmars-d-learn 
 <digitalmars-d-learn puremagic.com>
 wrote:

  From http://dlang.org/type, under Usual Arithmetic 
 Conversions:
 4. Else the integer promotions are done on each operand, 
 followed
 by:
      1. If both are the same type, no more conversions are 
 done.
it's bug in specs, i believe, 'cause compiler promotes smaller types to int/uint.
I don't understand. Why is this behavior preferrable to the one outlined by the specs? Thanks, Shachar
Sep 17 2014
prev sibling parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Wed, 17 Sep 2014 16:36:41 +0300
Shachar Shemesh via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:

 I don't understand. Why is this behavior preferrable to the one
 outlined by the specs?
'cause C does exactly this. there is no reason to confuse people with C background by silently removing such feature.
Sep 17 2014
prev sibling parent "anonymous" <anonymous example.com> writes:
On Wednesday, 17 September 2014 at 13:20:15 UTC, Shachar wrote:
 On Wednesday, 17 September 2014 at 13:03:05 UTC, 
 flamencofantasy wrote:
 the result of ubyte + ubyte is int I believe.

 Try;
 void func( int c )
 {
     ubyte a;

     a = cast(ubyte)(cast(ubyte)c + cast(ubyte)c);
 }
From http://dlang.org/type, under Usual Arithmetic Conversions: 4. Else the integer promotions are done on each operand, followed by: 1. If both are the same type, no more conversions are done. So, as far as I can see, the specs disagree with you.
You missed that "integer promotions" are done first. <http://dlang.org/type.html#Integer%20Promotions>:
 Integer Promotions are conversions of the following types:
[...]
 from	to
[...]
 ubyte	int
[...] So, integer promotions turn ubyte + ubyte into int + int.
Sep 17 2014
prev sibling parent "flamencofantasy" <flamencofantasy gmail.com> writes:
http://www.drdobbs.com/tools/value-range-propagation/229300211
Sep 17 2014