toResultEager method

Result<(A, B, C), Z> toResultEager()

Transforms a Record of Results into a single Result. The Ok value is a Record of all Result's Ok values. The Err value is the first Err encountered.

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()).toResultEager()){
      case Ok(:final ok):
        (a, b, c) = ok;
      case Err():
        throw Exception();
    }

Implementation

Result<(A, B, C), Z> toResultEager() {
  A a;
  if ($1.isOk()) {
    a = $1.unwrap();
  } else {
    return $1.intoUnchecked();
  }
  B b;
  if ($2.isOk()) {
    b = $2.unwrap();
  } else {
    return $2.intoUnchecked();
  }
  C c;
  if ($3.isOk()) {
    c = $3.unwrap();
  } else {
    return $3.intoUnchecked();
  }

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