ensure method
Ensure check if the value
is true
,
and if it is it allows the Either.binding(...)
to continue.
In case it is false
, then it short-circuits the binding and returns
the provided value by orLeft
inside a Left.
See Either.binding and Either.futureBinding.
Example
final res = Either<String, int>.binding((e) {
e.ensure(true, () => '');
print('ensure(true) passes');
e.ensure(false, () => 'failed');
return 1;
});
// print: 'ensure(true) passes'
// res: Left('failed')
Implementation
@monadComprehensions
void ensure(bool value, L Function() orLeft) =>
value ? null : bind(orLeft().left());