Is there any reason why arithmetic operation on shorts and bytes return int?

Simen Kjaeraas simen.kjaras at gmail.com
Wed Dec 12 01:42:31 PST 2012


On 2012-17-12 10:12, Jonathan M Davis <jmdavisProg at gmx.com> wrote:

> On Wednesday, December 12, 2012 09:23:35 Simen Kjaeraas wrote:
>> On 2012-09-12 00:12, js.mdnq <js_adddot+mdng at gmail.com> wrote:
>> > struct bbyte {
>> > byte value;
>> > ...
>> > }
>> >
>> > bbyte a; bbyte b;
>> >
>> > b = a + b; // uses bbyte's operators and casts to do the computation  
>> and
>> > assignment but then returns a bbyte instead of an int.
>> >
>> > You should have no problems implicitly converting bbyte to built in
>> > types or built in types to bbyte.
>>
>> Not entirely true. Converting from bbyte to built-in works, but these
>> are to my knowledge currently impossible:
>>
>> void foo(bbyte b);
>> byte b;
>>
>> foo(b); // No conversion.
>>
>>
>> bbyte bar( ) {
>>      byte b;
>>      return b;
>> }
>
> You can do it with alias this, but the current lack of ability to have
> multiple alias thises probably makes it so that you can't use it for
> converting in both directions unless you want to directly alias it to the
> member variable holding the value (which eans no checks or whatever else  
> you
> might want to do in bbyte). Once we can have multiple alias thises  
> though,
> that shouldn't be problem anymore.

Really? This certainly does not compile for me:


struct bbyte {
     byte b;
     alias b this;
}

void bar(bbyte b) {}

bbyte baz() {
     byte b;
     return b; // cannot implicitly convert expression (b) of type byte to  
bbyte
}

void main() {
     byte b;
     bar(b); // function bar (bbyte b) is not callable using argument types  
(byte)
}


This is also the reason for bug #8570

http://d.puremagic.com/issues/show_bug.cgi?id=8570

-- 
Simen


More information about the Digitalmars-d mailing list