pipe<T2> method

Result<T2> pipe<T2>(
  1. dynamic f(
    1. T
    )
)

If result is Err then returns this Err as is. If result is Ok then returns result of call f(result.val). If result of call f() is null - returns Err('function returned null'). If result of call f() is Result - returns it as is. If call of f() throws exception, then returns Err.

Implementation

Result<T2> pipe<T2>(Function(T) f) {
  if (isErr) {
    return Err<T2>(err);
  }
  var argument = unwrap;
  try {
    var res = f(argument);
    if (res == null) {
      return Err<T2>(
          StateError('Result of f($argument) == null, where `f` is $f'));
    } else {
      if (res is Error) {
        return Err<T2>(res);
      } else {
        if (res is Ok) {
          return Ok<T2>(res.unwrap);
        } else if (res is Err) {
          return Err<T2>(res.err!);
        } else {
          return Ok<T2>(res);
        }
      }
    }
  } catch (e) {
    return Err(StateError(e.toString()));
  }
}