error

Constructs a new Result with the status set to error and with the provided value.

If you don't specify the type of the okay value for this then it is assumed to be the same as the error type.

@safe @nogc static
Result!(OkayType, ErrorType)
error
(
ErrorType
OkayType = ErrorType
)
(
ErrorType errorVal
)

Parameters

errorVal ErrorType

the error value

Return Value

Type: Result!(OkayType, ErrorType)

a Result

Examples

Tests the usage of okay result types

auto a = ok("A successful result");
assert(a.ok == "A successful result");
assert(a.error == null);

// Should be Result!(string, string)
static assert(__traits(isSame, typeof(a.okay_val), string));
static assert(__traits(isSame, typeof(a.error_val), string));

// opCast to bool
assert(cast(bool)a);

// Validity checking
assert(a.is_okay());
assert(!a.is_error());

auto b = ok!(string, Exception)("A successful result");
assert(b.ok == "A successful result");
assert(b.error is null);

// Should be Result!(string, Exception)
static assert(__traits(isSame, typeof(b.okay_val), string));
static assert(__traits(isSame, typeof(b.error_val), Exception));

Tests the usage of error result types

auto a = error(new Exception("A failed result"));
assert(a.ok is null);
assert(cast(Exception)a.error && (cast(Exception)a.error).msg == "A failed result");

// Should be Result!(Exception, Exception)
static assert(__traits(isSame, typeof(a.okay_val), Exception));
static assert(__traits(isSame, typeof(a.error_val), Exception));

// opCast to bool
assert(!cast(bool)a);

// Validity checking
assert(!a.is_okay());
assert(a.is_error());

auto b = error!(Exception, string)(new Exception("A failed result"));
assert(a.ok is null);
assert(cast(Exception)a.error && (cast(Exception)a.error).msg == "A failed result");

// Should be Result!(string, Exception)
static assert(__traits(isSame, typeof(b.okay_val), string));
static assert(__traits(isSame, typeof(b.error_val), Exception));

Meta