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

Jonathan M Davis jmdavisProg at gmx.com
Wed Dec 12 01:17:50 PST 2012


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.

- Jonathan M Davis


More information about the Digitalmars-d mailing list