toResult method

Result<(A, B, C, D, E), Z> toResult()

Transforms a Record of Result functions into a single Result. The Ok value is a Record of all Result's Ok values. The Err value is first function that evaluates to an Err.

Instead of writing code like this

    final a, b, c;
    final boolResult = boolOk();
    switch(boolResult){
      case Ok(:final ok):
        a = ok;
      case Err():
        return boolResult;
    }
    final intResult = intOk();
    switch(intResult){
      case Ok(:final ok):
        b = ok;
      case Err():
        return intResult;
    }
    final doubleResult = doubleOk();
    switch(doubleResult){
      case Ok(:final ok):
        c = ok;
      case Err():
        return doubleResult;
    }

You can now write it like this

    final a, b, c;
    switch((boolOk, intOk, doubleOk).toResult()){
      case Ok(:final ok):
        (a, b, c) = ok;
      case Err():
        throw Exception();
    }

Implementation

Result<(A, B, C, D, E), Z> toResult() {
  A a;
  final aResult = $1();
  if (aResult.isOk()) {
    a = aResult.unwrap();
  } else {
    return aResult.intoUnchecked();
  }
  B b;
  final bResult = $2();
  if (bResult.isOk()) {
    b = bResult.unwrap();
  } else {
    return bResult.intoUnchecked();
  }
  C c;
  final cResult = $3();
  if (cResult.isOk()) {
    c = cResult.unwrap();
  } else {
    return cResult.intoUnchecked();
  }
  D d;
  final dResult = $4();
  if (dResult.isOk()) {
    d = dResult.unwrap();
  } else {
    return dResult.intoUnchecked();
  }
  E e;
  final eResult = $5();
  if (eResult.isOk()) {
    e = eResult.unwrap();
  } else {
    return eResult.intoUnchecked();
  }

  return Ok((a, b, c, d, e));
}