Implicit integer casting

Manu turkeyman at gmail.com
Sun Mar 18 12:08:44 PDT 2012


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20120318/ef6ee029/attachment.html>


More information about the Digitalmars-d mailing list