toResult method

Result<(A, B, C, D, E, F, G, H, I, J), List<Z>> toResult()

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 List of all Err values.

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, F, G, H, I, J), List<Z>> toResult() {
  List<Z> z = [];
  A? a;
  if ($1.isOk()) {
    a = $1.unwrap();
  } else {
    z.add($1.unwrapErr());
  }
  B? b;
  if ($2.isOk()) {
    b = $2.unwrap();
  } else {
    z.add($2.unwrapErr());
  }
  C? c;
  if ($3.isOk()) {
    c = $3.unwrap();
  } else {
    z.add($3.unwrapErr());
  }
  D? d;
  if ($4.isOk()) {
    d = $4.unwrap();
  } else {
    z.add($4.unwrapErr());
  }
  E? e;
  if ($5.isOk()) {
    e = $5.unwrap();
  } else {
    z.add($5.unwrapErr());
  }
  F? f;
  if ($6.isOk()) {
    f = $6.unwrap();
  } else {
    z.add($6.unwrapErr());
  }
  G? g;
  if ($7.isOk()) {
    g = $7.unwrap();
  } else {
    z.add($7.unwrapErr());
  }
  H? h;
  if ($8.isOk()) {
    h = $8.unwrap();
  } else {
    z.add($8.unwrapErr());
  }
  I? i;
  if ($9.isOk()) {
    i = $9.unwrap();
  } else {
    z.add($9.unwrapErr());
  }
  J? j;
  if ($10.isOk()) {
    j = $10.unwrap();
  } else {
    z.add($10.unwrapErr());
  }

  if (z.isEmpty) {
    return Ok((a!, b!, c!, d!, e!, f!, g!, h!, i!, j!));
  } else {
    return Err(z);
  }
}