toResultEager method

Result<(A, B, C, D, E, F, G, H), 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, D, E, F, G, H), 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();
  }

  D d;
  if ($4.isOk()) {
    d = $4.unwrap();
  } else {
    return $4.intoUnchecked();
  }

  E e;
  if ($5.isOk()) {
    e = $5.unwrap();
  } else {
    return $5.intoUnchecked();
  }

  F f;
  if ($6.isOk()) {
    f = $6.unwrap();
  } else {
    return $6.intoUnchecked();
  }

  G g;
  if ($7.isOk()) {
    g = $7.unwrap();
  } else {
    return $7.intoUnchecked();
  }

  H h;
  if ($8.isOk()) {
    h = $8.unwrap();
  } else {
    return $8.intoUnchecked();
  }

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