Function tryUntil
tryUntil will take a number of lazy expressions and execute them in order until they all pass or one of them fails
auto auto tryUntil(T...)
(
lazy T expressions
);
auto auto tryUntil(T...)
(
lazy T expressions
);
Parameters
Name | Description |
---|---|
expressions | a variadic list of expressions |
Returns
The result is a Try
that either has a success value of a Tuple
of results
or an Exception
Since
0.16.0
Example
import std .conv: to;
import std .algorithm: map, each;
import std .typecons: Tuple, tuple;
import ddash .utils .match: match;
int f(int i) {
if (i % 2 == 1) {
throw new Exception("uneven int");
}
return i;
}
string g(int i) {
if (i % 2 == 1) {
throw new Exception("uneven string");
}
return i .to!string;
}
auto r0 = tryUntil(f(2), g(2)); // both succeed
assert(r0 .front == tuple(2, "2"));
auto r1 = tryUntil(f(1), g(2)); // first one fails
const s1 = r1 .match!((_) => "?", ex => ex .msg);
assert(s1 == "uneven int");
auto r2 = tryUntil(f(2), g(1)); // second one fails
const s2 = r2 .match!((_) => "?", ex => ex .msg);
assert(s2 == "uneven string");