Thread overview
Why does this not work?
Sep 17, 2014
Shachar
Sep 17, 2014
flamencofantasy
Sep 17, 2014
Shachar
Sep 17, 2014
ketmar
Sep 17, 2014
Shachar Shemesh
Sep 17, 2014
flamencofantasy
Sep 17, 2014
ketmar
Sep 17, 2014
anonymous
Sep 17, 2014
flamencofantasy
September 17, 2014
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
September 17, 2014
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);
}
September 17, 2014
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
September 17, 2014
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.


September 17, 2014
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
September 17, 2014
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

September 17, 2014
http://www.drdobbs.com/tools/value-range-propagation/229300211


September 17, 2014
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.


September 17, 2014
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.