toResult method
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), 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());
}
if (z.isEmpty) {
return Ok((a!, b!, c!, d!, e!));
} else {
return Err(z);
}
}