toResult method

Result<(A, B), 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), 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());
  }

  if (z.isEmpty) {
    return Ok((a!, b!));
  } else {
    return Err(z);
  }
}