Result<S, F extends Object> constructor
Result<S, F extends Object> (
- _ResultEarlyReturnFunction<
S, F> fn
Creates a context for early return, similar to "Do notation". Here "$" is used as the "Early Return Key". when "$" is used on a type Err, immediately the context that "$" belongs to is returned with that Err. Works like the Rust "?" operator, which is a "Early Return Operator". e.g.
Result<int,String> innerFn() => Err("message");
Result<int, String> innerFn2() => Ok(1);
Result<int, String> earlyReturn() => Result(($) {
int y = 2;
int x = innerFn()[$]; // returns [Err] here immediately
int z = innerFn2()[$]; // innerFn2 will not be executed
return Ok(x + y + z);
});
}
expect(earlyReturn().unwrapErr(), "message");
This should be used at the top level of a function as above. Passing "$" to any other functions, nesting, or attempting to bring "$" out of the original scope should be avoided.
Implementation
factory Result(_ResultEarlyReturnFunction<S, F> fn) {
try {
return fn(_ResultEarlyReturnKey<F>._());
} on _ResultEarlyReturnNotification<F> catch (notification) {
return notification.value;
}
}