The OpenD Programming Language

Checked.opUnary

Defines unary operators +, -, ~, ++, and --. Unary + is not overridable and always has built-in behavior (returns this). For the others, if Hook defines hookOpUnary, opUnary forwards to Checked!(typeof(hook.hookOpUnary!op(get)), Hook)(hook.hookOpUnary!op(get)).

If Hook does not define hookOpUnary but defines onOverflow, opUnary forwards to hook.onOverflow!op(get) in case an overflow occurs. For ++ and --, the payload is assigned from the result of the call to onOverflow.

Note that unary - is considered to overflow if T is a signed integral of 32 or 64 bits and is equal to the most negative value. This is because that value has no positive negation.

  1. auto opUnary()
  2. Checked opUnary()
    struct Checked(T, Hook = Abort)
    ref return
    opUnary
    (
    string op
    )
    ()
    if (
    op == "++" ||
    op == "--"
    )
    if (
    is(T == Checked!(U, H),
    U
    H
    )
    )

Parameters

op

The unary operator

Return Value

Type: Checked

A Checked instance representing the result of the unary operation

Examples

static struct MyHook
{
    static bool thereWereErrors;
    static L hookOpUnary(string x, L)(L lhs)
    {
        if (x == "-" && lhs == -lhs) thereWereErrors = true;
        return -lhs;
    }
}
auto a = checked!MyHook(long.min);
assert(a == -a);
assert(MyHook.thereWereErrors);
auto b = checked!void(42);
assert(++b == 43);

Meta