digitalmars.D - Implicit integer casting
- Manu <turkeyman gmail.com> Mar 18 2012
- "Tove" <tove fransson.se> Mar 18 2012
- Manu <turkeyman gmail.com> Mar 18 2012
- =?utf-8?Q?Simen_Kj=C3=A6r=C3=A5s?= <simen.kjaras gmail.com> Mar 18 2012
--20cf3074b3dab26e8904bb892b51 Content-Type: text/plain; charset=UTF-8 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. --20cf3074b3dab26e8904bb892b51 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <font face=3D"arial, helvetica, sans-serif">So D is really=C2=A0finicky=C2= =A0with integer casts. Basically everything that might produce a loss of da= ta warning in C is an outright compile error.</font><div><font face=3D"aria= l, helvetica, sans-serif">This results in a lot of explicit casting.</font>= </div> <div><font face=3D"arial, helvetica, sans-serif"><br></font></div><div><fon= t face=3D"arial, helvetica, sans-serif">Now I don't take issue with thi= s, actually I think it's awesome, but I think there's one very impo= rtant usability feature missing from the compiler with such strict casting = rules...</font></div> <div><font face=3D"arial, helvetica, sans-serif">Does the compiler currentl= y track the range of a value, if it is known? And if it is known, can the c= ompiler stop complaining about down casts and perform the cast silently whe= n it knows the range of values is safe.</font></div> <div><font face=3D"arial, helvetica, sans-serif"><br></font></div><div><fon= t face=3D"arial, helvetica, sans-serif">int x =3D 123456;</font></div><div>= <font face=3D"arial, helvetica, sans-serif">x &=3D 0xFF; // x is now in= range 0..255; now fits in a ubyte</font></div> <div><font face=3D"arial, helvetica, sans-serif">ubyte y =3D x; // assign s= ilently, cast can safely be implicit</font></div><div><font face=3D"arial, = helvetica, sans-serif"><br></font></div><div><font face=3D"arial, helvetica= , sans-serif">I have about 200 lines of code that would be so much more rea= dable if this were supported.</font></div> <div><font face=3D"arial, helvetica, sans-serif">I'm finding that in th= is 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 obsc= uring the=C2=A0readability.</font></div> <div><font face=3D"arial, helvetica, sans-serif">Not only masks, comparison= s 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 t= he range of integers, and it should consider that while performing casts.</= font></div> --20cf3074b3dab26e8904bb892b51--
Mar 18 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
Mar 18 2012
--20cf3005dc229e73cb04bb8978d6 Content-Type: text/plain; charset=UTF-8 On 18 March 2012 21:15, Tove <tove fransson.se> wrote: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<http://drdobbs.com/blogs/tools/229300211>
Interesting. This article claims: Can we do better? Yes, with "Value Range Propagation", a historically obscure compiler optimization that became a handy feature in the D programming language. But it doesn't seem to work. Am I just doing something wrong? --20cf3005dc229e73cb04bb8978d6 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On 18 March 2012 21:15, Tove <span dir=3D"ltr">&= lt;<a href=3D"mailto:tove fransson.se">tove fransson.se</a>></span> wrot= e:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-l= eft:1px #ccc solid;padding-left:1ex"> <div class=3D"im">On Sunday, 18 March 2012 at 19:08:54 UTC, Manu wrote:<br> </div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-l= eft:1px #ccc solid;padding-left:1ex"><div class=3D"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, bu= t 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=3D"im"> int x =3D 123456;<br> x &=3D 0xFF; // x is now in range 0..255; now fits in a ubyte<br> ubyte y =3D x; // assign silently, cast can safely be implicit<br> <br></div><div class=3D"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=3D"http://drdobbs.com/blogs/tools/229300211" target=3D"_blank">http= ://drdobbs.com/blogs/<u></u>tools/229300211</a></blockquote><div><br></div>= <div>Interesting. This article claims:=C2=A0<span style=3D"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 hi= storically obscure compiler optimization that became a handy feature in the= D programming language.</span>=C2=A0</div> <div><br></div><div>But it doesn't seem to work. Am I just doing someth= ing wrong?</div></div> --20cf3005dc229e73cb04bb8978d6--
Mar 18 2012
On Sun, 18 Mar 2012 20:30:15 +0100, Manu <turkeyman gmail.com> wrote:On 18 March 2012 21:15, Tove <tove fransson.se> wrote: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<http://drdobbs.com/blogs/tools/229300211>
Interesting. This article claims: Can we do better? Yes, with "Value Range Propagation", a historically obscure compiler optimization that became a handy feature in the D programming language. But it doesn't seem to work. Am I just doing something wrong?
It only works within one expression. This works: int n = foo(); byte b = n & 0xFF; This does not: int n = foo() & 0xFF; byte b = n;
Mar 18 2012









"Tove" <tove fransson.se> 