<div class="gmail_quote">On 18 March 2012 21:15, Tove <span dir="ltr"><<a href="mailto:tove@fransson.se">tove@fransson.se</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">On Sunday, 18 March 2012 at 19:08:54 UTC, Manu wrote:<br>
</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
So D is really finicky with integer casts. Basically everything that might<br>
produce a loss of data warning in C is an outright compile error.<br>
This results in a lot of explicit casting.<br>
<br>
Now I don't take issue with this, actually I think it's awesome, but I<br>
think there's one very important usability feature missing from the<br>
compiler with such strict casting rules...<br>
Does the compiler currently track the range of a value, if it is known? And<br>
if it is known, can the compiler stop complaining about down casts and<br>
perform the cast silently when it knows the range of values is safe.<br>
<br></div><div class="im">
int x = 123456;<br>
x &= 0xFF; // x is now in range 0..255; now fits in a ubyte<br>
ubyte y = x; // assign silently, cast can safely be implicit<br>
<br></div><div class="im">
I have about 200 lines of code that would be so much more readable if this<br>
were supported.<br>
I'm finding that in this code I'm writing, casts are taking up more space<br>
on many lines than the actual term being assigned. They are really getting<br>
in the way and obscuring the readability.<br>
Not only masks, comparisons are also often used of limit the range of<br>
values. Add D's contracts, there is good chance the compiler will have<br>
fairly rich information about the range of integers, and it should consider<br>
that while performing casts.<br>
</div></blockquote>
<br>
Walter even wrote an article about it:<br>
<a href="http://drdobbs.com/blogs/tools/229300211" target="_blank">http://drdobbs.com/blogs/<u></u>tools/229300211</a></blockquote><div><br></div><div>Interesting. This article claims: <span style="background-color:rgb(249,250,252);font-family:Verdana,Arial,Helvetica,sans-serif;font-size:13px">Can we do better? Yes, with "Value Range Propagation", a historically obscure compiler optimization that became a handy feature in the D programming language.</span> </div>
<div><br></div><div>But it doesn't seem to work. Am I just doing something wrong?</div></div>