and<U> method

Result<U, E> and<U>(
  1. Result<U, E> res
)

Returns res if the result is ok, otherwise returns the error value of class.

Example

Basic Usage:

import 'package:dartsult/dartsult.dart';

main() {
  Result<int, String> x = Result.ok(2);
  Result<String,String> y = Result.error("late error");

  assert(x.and(y) == Result.error<String, String>("late error"));

  x = Result.error("early error");
  y = Result.ok("foo");
  assert(x.and(y) == Result.error<String, String>("early error"));

  x = Result.error("not a 2");
  y = Result.error("late error");
  assert(x.and(y) == Result.error<String, String>("not a 2"));

  x = Result.ok(2);
  y = Result.ok("different result type");
  assert(x.and(y) == Result.ok<String, String>("different result type"));
}

Implementation

Result<U, E> and<U>(Result<U, E> res) {
  if (_error.val != null) {
    return Result.error(_error.val!);
  }
  return res;
}