Disable implicit conversion

Jesse Phillips jessekphillips+D at gmail.com
Mon Mar 7 10:12:53 PST 2011


Stewart Gordon Wrote:

> If overflow of a ubyte matters, surely so does overflow of an int or long?
> 
> In the OP's case, the only possible overflow is a negative result.  As such, if overflow 
> matters, one will do something like
> 
>      if (u2 < u1) {
>          // ...
>      } else {
>          u3 = cast(ubyte) (u2 - u1);
>      }
> 
> For such simple cases as this, using a library function to convert an overflow into an 
> exception might be overkill.
> 
> Stewart.

By matters I meant that you do not expect it to result in a negative number, but if it does the code will start operating incorrectly. There are cases where you may expect the result to go negative but don't care as it won't effect what you are doing (I don't actually have any examples of this but it is possible).

My main point is that if you are in the habit of using std.conv.to instead of casting then you are more likely to get a meaningful message at run-time then a program in invalid state. But you are correct that if you desire doing something with what would be an Exception then writing the explicit code is better.


More information about the Digitalmars-d mailing list