expectations 0.3.0

Error handling that bundles exceptions with return values

To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:


Error handling that bundles exceptions with return values.


  • Expected values can be treated like return codes or exceptions:
    • Use hasValue to check for success or failure explicitly.
    • Use value directly to assume success and throw in case of failure.
  • Error handling is deferred until the value is actually needed.
  • Functions that return Expected values can be composed easily using andThen and map.
  • Usable in @safe and nothrow code.


View online on Github Pages.

expectations uses adrdox to generate its documentation. To build your own copy, run the following command from the root of the sumtype repository:

path/to/adrdox/doc2 --genSearchIndex --genSource -o generated-docs src


import std.math: approxEqual;
import std.exception: assertThrown;
import std.algorithm: equal;

Expected!double relative(double a, double b)
    if (a == 0) {
        return unexpected!double(
            new Exception("Division by zero")
    } else {
        return expected((b - a)/a);

assert(relative(2, 3).hasValue);
assert(relative(2, 3).value.approxEqual(0.5));

assert(!relative(0, 1).hasValue);
assertThrown(relative(0, 1).value);
assert(relative(0, 1).exception.msg.equal("Division by zero"));


If you're using dub, add the expectations package to your project as a dependency.

Otherwise, you will need to add both expectations.d and its dependency sumtype.d (from the sumtype package) to your source directory.

  • Paul Backus
0.4.0 2019-Apr-25
0.3.0 2019-Feb-17
0.2.0 2018-Sep-04
0.1.0 2018-Sep-02
~master 2019-Apr-25
Show all 5 versions
Download Stats:
  • 0 downloads today

  • 0 downloads this week

  • 0 downloads this month

  • 21 downloads total

Short URL: