expect

Unwraps a result, yielding the content of expected value. If there is none, or error value, it throws assert(0) with the provided message.

  1. T expect(EX res, string msg)
    ref
    T
    expect
    (
    EX : Expected!(T, E, H)
    T
    E
    H
    )
    (
    auto ref EX res
    ,
    lazy string msg
    )
  2. T expect(EX res)

Parameters

res EX

Expected to check the result of

msg string

message to use with assert

Examples

assert(ok(42).expect("oops") == 42);
ok().expect("oops"); // void value

version (D_Exceptions)
{
    () @trusted
    {
        assert(collectExceptionMsg!Throwable(Expected!int.init.expect("oops")) == "oops: empty");
        assert(collectExceptionMsg!Throwable(err!int("foo").expect("oops")) == "oops: foo");
    }();
}

assert(ok("foo").expect!(a => "bar") == "foo");
assert(err!string("foo").expect!(a => "bar") == "bar");
assert(err!string("foo").expect!((a) {}) is null);

static struct NonCopyable { @disable this(this); int foo; }
assert(err!NonCopyable(42).expect!((e) {}) == NonCopyable.init);
err!void(42).expect!((e) {});
ok!int().expect!(e => assert(0));
ok!int(42).expect!(e => assert(0));

Meta