ensureNotNull<R extends Object> method

  1. @monadComprehensions
R ensureNotNull<R extends Object>(
  1. R? value,
  2. L orLeft()
)

Ensures that value is not null. When the value is not null, then it will be returned as non null and the check value is now smart-checked to non-null. Otherwise, if the value is null then the Either.binding(...) will short-circuit with orLeft inside of Left.

See Either.binding and Either.futureBinding.

Example

final res = Either<String, int>.binding((e) {
  int? x = 1;
  e.ensureNotNull(x, () => 'passes');
  print(x);

  x = null;
  e.ensureNotNull(x, () => 'failed');
  print(x);

  return 1;
});
// println: '1'
// res: Left('failed')

Implementation

@monadComprehensions
R ensureNotNull<R extends Object>(R? value, L Function() orLeft) =>
    value ?? bind(orLeft().left());