std.experimental.checkedint is ready for comments!

Robert burner Schadek via Digitalmars-d digitalmars-d at puremagic.com
Tue Jun 7 01:50:07 PDT 2016


As with many other languages (C, C++, Java, etc.), D's built-in 
integer data types are quite difficult to use correctly.

It is tempting to think of int, for example, as if it were an 
actual mathematical integer. Doing so, however leads to buggy 
code due to unintuitive behaviour like:

* Wrapped overflow
* Reinterpretation of signed values as unsigned in mixed 
expressions
* Floating Point Exceptions which aren't Exceptions and have 
nothing to do with
   floating point
* Formally "undefined behaviour" with some inputs for various 
operations

This checkedint module provides alternative operations and types 
(SafeInt,SmartInt) that protect the user from most difficulties 
of this sort, while maintaining good performance (provided that 
inlining and optimizations are enabled).

== SmartInt ==
SmartInt smartOp strive to actually give the mathematically 
correct answer whenever possible, rather than just signaling an 
error.

== SafeInt ==
SafeInt safeOp strive to match the behaviour of the basic 
integral types exactly, $(B except) that where the behaviour of 
the basic type is wrong, or very unintuitive, an error is 
signaled instead.


The main downsides to using checkedint are:

Some added friction when interfacing to non-checkedint-aware code.
Slower compilation and larger binaries.


PR: https://github.com/dlang/phobos/pull/4407
DUB: http://code.dlang.org/packages/checkedint

I will do the review management.


More information about the Digitalmars-d mailing list