Implicit integer casting

Tove tove at fransson.se
Sun Mar 18 12:15:55 PDT 2012


On Sunday, 18 March 2012 at 19:08:54 UTC, Manu wrote:
> So D is really finicky with integer casts. Basically everything 
> that might
> produce a loss of data warning in C is an outright compile 
> error.
> This results in a lot of explicit casting.
>
> Now I don't take issue with this, actually I think it's 
> awesome, but I
> think there's one very important usability feature missing from 
> the
> compiler with such strict casting rules...
> Does the compiler currently track the range of a value, if it 
> is known? And
> if it is known, can the compiler stop complaining about down 
> casts and
> perform the cast silently when it knows the range of values is 
> safe.
>
> int x = 123456;
> x &= 0xFF; // x is now in range 0..255; now fits in a ubyte
> ubyte y = x; // assign silently, cast can safely be implicit
>
> I have about 200 lines of code that would be so much more 
> readable if this
> were supported.
> I'm finding that in this code I'm writing, casts are taking up 
> more space
> on many lines than the actual term being assigned. They are 
> really getting
> in the way and obscuring the readability.
> Not only masks, comparisons are also often used of limit the 
> range of
> values. Add D's contracts, there is good chance the compiler 
> will have
> fairly rich information about the range of integers, and it 
> should consider
> that while performing casts.

Walter even wrote an article about it:
http://drdobbs.com/blogs/tools/229300211



More information about the Digitalmars-d mailing list