@@ -1485,6 +1485,9 @@ E expectErr(EX : Expected!(T, E, H), T, E, H)(auto ref EX res, lazy string msg)
1485
1485
Returns the error contained within the $(LREF Expected) _and then_ another value if there's no error.
1486
1486
This function can be used for control flow based on $(LREF Expected) values.
1487
1487
1488
+ Predicate can accept no arguments, variable arguments, or previous result value with additional variable arguments.
1489
+ It must return $(LREF Expected) wth the same error type. But can provide different value type.
1490
+
1488
1491
Params:
1489
1492
exp = The $(LREF Expected) to call andThen on
1490
1493
value = The value to return if there isn't an error
@@ -1500,8 +1503,6 @@ auto ref andThen(EX : Expected!(T, E, H), VEX : Expected!(VT, E, H), T, VT, E, H
1500
1503
// / ditto
1501
1504
auto ref andThen (alias pred, EX : Expected! (T, E, H), T, E, H, Args... )(auto ref EX exp, Args args)
1502
1505
{
1503
- import std.traits : TemplateArgsOf;
1504
-
1505
1506
static if (! is (T == void ) && is (typeof (pred(T.init, args)) : Expected! (VT , E, H), VT ))
1506
1507
{
1507
1508
static if (is (T == VT )) return exp.hasError ? exp : pred(exp.value, args);
@@ -1554,6 +1555,9 @@ unittest
1554
1555
Returns the value contained within the $(LREF Expected) _or else_ another value if there's an error.
1555
1556
This function can be used for control flow based on $(LREF Expected) values.
1556
1557
1558
+ Predicate can accept no arguments, variable arguments, or previous result error value with additional variable arguments.
1559
+ It must return $(LREF Expected) wth the same value type. But can provide different error value type.
1560
+
1557
1561
Params:
1558
1562
exp = The $(LREF Expected) to call orElse on
1559
1563
value = The value to return if there is an error
@@ -1571,9 +1575,19 @@ auto ref orElse(alias pred, EX : Expected!(T, E, H), T, E, H, Args...)(
1571
1575
{
1572
1576
static if (is (typeof (pred(args)) : T))
1573
1577
return exp.hasError ? pred(args) : exp.value;
1574
- else static if (is (typeof (pred(args)) : EX ))
1575
- return exp.hasError ? pred() : exp;
1576
- else static assert (0 , " Expecting predicate of same value type as source Expected or predicate of same Expected type" );
1578
+ else static if (is (typeof (pred(exp.error, args)) : T))
1579
+ return exp.hasError ? pred(exp.error, args) : exp.value;
1580
+ else static if (is (typeof (pred(args)) : Expected! (T, VE , H), VE ))
1581
+ {
1582
+ static if (is (E == VE )) return exp.hasError ? pred(args) : exp;
1583
+ else return exp.hasError ? pred(args) : ok! VE (exp.value);
1584
+ }
1585
+ else static if (is (typeof (pred(exp.error, args)) : Expected! (T, VE , H), VE ))
1586
+ {
1587
+ static if (is (E == VE )) return exp.hasError ? pred(exp.error, args) : exp;
1588
+ else return exp.hasError ? pred(exp.error, args) : ok! VE (exp.value);
1589
+ }
1590
+ else static assert (0 , " Expecting predicate of same value type" );
1577
1591
}
1578
1592
1579
1593
// /
@@ -1595,6 +1609,11 @@ unittest
1595
1609
1596
1610
// with args
1597
1611
assert (err! int (" foo" ).orElse! ((v) => v)(42 ) == 42 );
1612
+
1613
+ // with different error type
1614
+ assert (err! int (" foo" ).orElse! ((v) => ok! int (v))(42 ).value == 42 ); // string -> int
1615
+ assert (err! int (" foo" ).orElse! ((v) => err! int (v))(42 ).error == 42 );
1616
+ assert (err! int (" foo" ).orElse! ((e, v) => err! int (e.length + v))(42 ).error == 45 ); // with previous error
1598
1617
}
1599
1618
1600
1619
/+ +
0 commit comments