WithNaN

Hook that reserves a special value as a "Not a Number" representative. For signed integrals, the reserved value is T.min. For signed integrals, the reserved value is T.max.

The default value of a Checked!(X, WithNaN) is its NaN value, so care must be taken that all variables are explicitly initialized. Any arithmetic and logic operation involving at least on NaN becomes NaN itself. All of a == b, a < b, a > b, a <= b, a >= b yield false if at least one of a and b is NaN.

Members

Static functions

hookOpBinary
auto hookOpBinary(L lhs, R rhs)

Defines hooks for binary operators +, -, *, /, %, ^^, &, |, ^, <<, >>, and >>> for cases where a Checked object is the left-hand side operand. If lhs == WithNaN.defaultValue!Lhs, returns WithNaN.defaultValue!(typeof(lhs + rhs)) without evaluating the operand. Otherwise, evaluates the operand. If evaluation does not overflow, returns the result. Otherwise, returns WithNaN.defaultValue!(typeof(lhs + rhs)).

hookOpBinaryRight
auto hookOpBinaryRight(L lhs, R rhs)

Defines hooks for binary operators +, -, *, /, %, ^^, &, |, ^, <<, >>, and >>> for cases where a Checked object is the right-hand side operand. If rhs == WithNaN.defaultValue!Rhs, returns WithNaN.defaultValue!(typeof(lhs + rhs)) without evaluating the operand. Otherwise, evaluates the operand. If evaluation does not overflow, returns the result. Otherwise, returns WithNaN.defaultValue!(typeof(lhs + rhs)).

hookOpCast
Lhs hookOpCast(Rhs rhs)

If rhs is WithNaN.defaultValue!Rhs, returns WithNaN.defaultValue!Lhs. Otherwise, returns cast(Lhs) rhs.

hookOpCmp
double hookOpCmp(Lhs lhs, Rhs rhs)

If lhs == WithNaN.defaultValue!Lhs, returns double.init. Otherwise, has the same semantics as the default comparison.

hookOpEquals
bool hookOpEquals(Lhs lhs, Rhs rhs)

Returns false if lhs == WithNaN.defaultValue!Lhs, lhs == rhs otherwise.

hookOpOpAssign
void hookOpOpAssign(L lhs, R rhs)

Defines hooks for binary operators +=, -=, *=, /=, %=, ^^=, &=, |=, ^=, <<=, >>=, and >>>= for cases where a Checked object is the left-hand side operand. If lhs == WithNaN.defaultValue!Lhs, no action is carried. Otherwise, evaluates the operand. If evaluation does not overflow and fits in Lhs without loss of information or change of sign, sets lhs to the result. Otherwise, sets lhs to WithNaN.defaultValue!Lhs.

hookOpUnary
auto hookOpUnary(T v)

Defines hooks for unary operators -, ~, ++, and --.

Static variables

defaultValue
enum T defaultValue(T);

The default value used for values not explicitly initialized. It is the NaN value, i.e. T.min for signed integrals and T.max for unsigned integrals.

max
enum T max(T);
min
enum T min(T);

The maximum value representable is T.max for signed integrals, T.max - 1 for unsigned integrals. The minimum value representable is T.min + 1 for signed integrals, 0 for unsigned integrals.

Examples

auto x1 = Checked!(int, WithNaN)();
assert(x1.isNaN);
assert(x1.get == int.min);
assert(x1 != x1);
assert(!(x1 < x1));
assert(!(x1 > x1));
assert(!(x1 == x1));
++x1;
assert(x1.isNaN);
assert(x1.get == int.min);
--x1;
assert(x1.isNaN);
assert(x1.get == int.min);
x1 = 42;
assert(!x1.isNaN);
assert(x1 == x1);
assert(x1 <= x1);
assert(x1 >= x1);
static assert(x1.min == int.min + 1);
x1 += long(int.max);

Meta