Could that bug be catch using D's way?

Basile B. b2.temp at gmx.com
Mon Feb 19 14:20:16 UTC 2018


On Monday, 19 February 2018 at 13:51:50 UTC, Simen Kjærås wrote:
> On Monday, 19 February 2018 at 13:33:34 UTC, rikki cattermole 
> wrote:
>> https://dlang.org/phobos/std_experimental_checkedint.html#.Checked.min
>
> Can't seem to get that to work, so I assumed it's not meant to 
> be used that way:
>
> import std.experimental.checkedint;
>
> struct MyHook {
>     enum min(T) = 3;
>     enum max(T) = 15;
>
>     static B onLowerBound(T, B)(T value, B bound)
>     {
>         assert(0);
>     }
>
>     static B onUpperBound(T, B)(T value, B bound)
>     {
>         assert(0);
>     }
> }
>
> unittest
> {
>     Checked!(int, MyHook) a;
>     a = 22;
>     assert(a != 22); // This assert triggers, not the others.
> }
>
> --
>   Simen

I had never used Checked and i discover that strangely there's no 
hook for opAssign. onLowerBound and onUpperBound works for +=, 
-=, *=, /=, %=, ^^=, &=, |=, ^=, <<=, >>=, and >>>=. But since 
init is 0, += works:

struct MyHook {
     enum min(T) = 3;
     enum max(T) = 15;

     static B onLowerBound(T, B)(T value, B bound)
     {
         assert(0);
     }

     static B onUpperBound(T, B)(T value, B bound)
     {
         assert(0);
     }
}

unittest
{
     Checked!(int, MyHook) a;
     a += 16; // triggers the onUpperBound assert
}


More information about the Digitalmars-d-learn mailing list