core.checkedint

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Fri Jun 24 18:42:41 PDT 2016


On 06/24/2016 08:11 PM, Walter Bright wrote:
> On 6/24/2016 4:59 PM, Andrei Alexandrescu wrote:
>> On 06/24/2016 07:56 PM, Walter Bright wrote:
>>> I believe adding such behavior is beyond the charter of checkedint.
>>
>> Which checkedint? The one in core? -- Andrei
>
> Your Checked!int. Sorry for the confusion.

Though you should be able to implement that via a hook, I think at this 
point we need to agree to disagree. The relevant code is at 
https://gist.github.com/andralex/a0c0ad32704e6ba66e458ac48add4a99#file-checked-d-L88:

     static if (!isUnsigned!L)
     {
         if (lhs < 0)
         {
             return subu(Result(rhs), Result(negs(lhs, overflow)),
                 overflow);
         }
     }

With your suggestion, this would also be an overflow:

long x = -1;
auto y = array.length + x;

I would be hard pressed to acknowledge that as an overflow that needs to 
be dynamically signaled. And the beauty of two's complement is that 
indeed it just works.


Andrei



More information about the Digitalmars-d mailing list