Skip to content

Monadic error handling for the D programming language

License

Notifications You must be signed in to change notification settings

pbackus/expectations

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

77 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

expectations

Error handling that bundles exceptions with return values.

Features

  • 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.

Documentation

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

Example

import std.exception: assertThrown;

Expected!int charToDigit(char c)
{
    int d = c - '0';
    if (d >= 0 && d < 10) {
        return expected(d);
    } else {
        return missing!int(
            new Exception(c ~ " is not a valid digit")
        );
    }
}

auto goodResult = charToDigit('7');
auto badResult = charToDigit('&');

assert(goodResult.hasValue);
assert(goodResult.value == 7);

assert(!badResult.hasValue);
assertThrown(badResult.value);
assert(badResult.error.msg == "& is not a valid digit");

Installation

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.

About

Monadic error handling for the D programming language

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages