Do you like bounded integrals?

tsbockman via Digitalmars-d digitalmars-d at puremagic.com
Tue Aug 30 13:03:17 PDT 2016


On Tuesday, 30 August 2016 at 14:58:16 UTC, Chris Wright wrote:
> On Tue, 30 Aug 2016 13:24:04 +0000, tsbockman wrote:
>> Ranges don't always grow. Some operations will also cause them 
>> to shrink, if they're really being tracked correctly:
>
> We can only track types, not values, and that strongly hampers 
> our ability to reduce ranges. We can still do it sometimes, but 
> no operation always reduces ranges (and every operation 
> sometimes extends ranges).

Then don't try to propagate the ranges at all.

Use regular `CheckedInt` as the return type for all of 
`BoundInt`'s non-assignment binary operations. Make the user 
explicitly label which variables should be checked against what 
ranges, instead of automatically inserting the usually-wrong 
guess that every intermediate value belongs in the same narrow 
range as the inputs. Otherwise, `BoundInt` will generate tons of 
spurious exceptions, particularly for multiplication.

Expressions that are complicated enough that custom range-based 
sanity checks need to be done inside of it, on rvalues, should 
probably be broken up into smaller pieces, anyway.


More information about the Digitalmars-d mailing list