Expect

The expect type can be used to return values and error codes from functions

The "expected" value is the success value and the unexpected (which is also typed as Unexpected) is the failure value.

The default value is always the init value of the expected case

Constructors

this
this(Expected value)
this(Unexpected value)

Constructor takes a Expected and creates a success result. Or takes an E and creates an unexpected result

Alias This

get

Members

Functions

isExpected
bool isExpected()

Returns true if the value is expected

match
auto match()

Pass in 2 handlers, one that handles Expected` and another that handles Unexpected

opEquals
bool opEquals(Expected rhs)
bool opEquals(Unexpected rhs)
bool opEquals(AnyUnexpected)

compares a value or an unexpected value. To compare with an unepexted value you must used either Unexpected!E as the rhs or it's type contructor.

toString
string toString()

Calls std.conv.to!string on T or E

Static functions

expected
expected(auto ref V value)

Create an Expect with an expected value

unexpected
unexpected(auto ref V value)

Create an Expect with an unexpected value

Examples

1 Expect!int toInt(string str) {
2     alias Result = typeof(return);
3     import std.conv: to;
4     try {
5         return Result.expected(str.to!int);
6     } catch (Exception ex) {
7         return Result.unexpected(ex.msg);
8     }
9 }
10 
11 assert(toInt("33") == 33);
12 assert(toInt("!33") == anyUnexpected);

Meta