toResult method
Transforms a Record of Result functions into a single Result. The Ok value is a Record of all Result's Ok values. The Err value is first function that evaluates to an Err.
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), Z> toResult() {
A a;
final aResult = $1();
if (aResult.isOk()) {
a = aResult.unwrap();
} else {
return aResult.intoUnchecked();
}
B b;
final bResult = $2();
if (bResult.isOk()) {
b = bResult.unwrap();
} else {
return bResult.intoUnchecked();
}
C c;
final cResult = $3();
if (cResult.isOk()) {
c = cResult.unwrap();
} else {
return cResult.intoUnchecked();
}
D d;
final dResult = $4();
if (dResult.isOk()) {
d = dResult.unwrap();
} else {
return dResult.intoUnchecked();
}
E e;
final eResult = $5();
if (eResult.isOk()) {
e = eResult.unwrap();
} else {
return eResult.intoUnchecked();
}
F f;
final fResult = $6();
if (fResult.isOk()) {
f = fResult.unwrap();
} else {
return fResult.intoUnchecked();
}
G g;
final gResult = $7();
if (gResult.isOk()) {
g = gResult.unwrap();
} else {
return gResult.intoUnchecked();
}
H h;
final hResult = $8();
if (hResult.isOk()) {
h = hResult.unwrap();
} else {
return hResult.intoUnchecked();
}
I i;
final iResult = $9();
if (iResult.isOk()) {
i = iResult.unwrap();
} else {
return iResult.intoUnchecked();
}
J j;
final jResult = $10();
if (jResult.isOk()) {
j = jResult.unwrap();
} else {
return jResult.intoUnchecked();
}
return Ok((a, b, c, d, e, f, g, h, i, j));
}