std.experimental.checkedint is ready for comments!

tsbockman via Digitalmars-d digitalmars-d at puremagic.com
Wed Jun 15 16:34:11 PDT 2016


On Wednesday, 15 June 2016 at 16:40:19 UTC, Andrei Alexandrescu 
wrote:
> One of the first things I looked for was establishing bounds 
> for numbers, like Smart!(int, 0, 100) for percentage. For all 
> its might, this package does not offer this basic facility, and 
> from what I can tell does not allow users to enforce it via 
> policies.

Another angle on why `checkedint` does not currently include a 
bounded type: although some of the internals might be similar, 
conceptually a `BoundInt` type is actually the *opposite* of the 
ideal which `SmartInt` and `SafeInt` strive toward.

The problem which this package was written to solve, is that when 
programming people frequently want to use "integers" as a data 
type. A real "integer" is, by definition, UNbounded, with an 
infinite range of values available. This makes designing 
algorithms simpler, but of course leads to problems when a value 
strays outside the narrow range in which the behaviour of machine 
integers correctly emulates that of mathematical integers.

The whole point of `checkedint` is to be able to write algorithms 
based on the simplifying assumption that your variables behave 
like real, UNbounded mathematical integers, secure in the 
knowledge that an error message will be generated if that 
assumption is invalidated.

Intentionally limiting values to a specific range is a different 
problem, and never the one that I was trying to solve - although 
I agree that the `checkedint` package would be a good namespace 
in which to park a `BoundInt` type, if someone wants to submit 
one.



More information about the Digitalmars-d mailing list