A result type (based on swiftz) that can represent either an error or success:
enum Result<X, T> {
case Err(() -> X)
case Ok(() -> T)
}
Now we need a way to chain multiple results together without lots of nesting of if statements -- or exceptions. To do so, we can define a new bind (result, next)
operator (implementation borrowed from swiftz) that operates on Result
types (a.k.a flatMap
or >>=
):
- If the result is
Err
, the result is immediately returned.