@@ -28,11 +28,33 @@ object Result:
28
28
case Ok (x) => f(x)
29
29
case err : Err [_] => err
30
30
31
+ /** Validate both `r` and `other`; return a pair of successes or a list of failures. */
32
+ def * [U ](other : Result [U , E ]): Result [(T , U ), List [E ]] = (r, other) match
33
+ case (Ok (x), Ok (y)) => Ok ((x, y))
34
+ case (Ok (_), Err (e)) => Err (e :: Nil )
35
+ case (Err (e), Ok (_)) => Err (e :: Nil )
36
+ case (Err (e1), Err (e2)) => Err (e1 :: e2 :: Nil )
37
+
38
+ /** Validate both `r` and `other`; return a tuple of successes or a list of failures.
39
+ * Unlike with `*`, the right hand side `other` must be a `Result` returning a `Tuple`,
40
+ * and the left hand side is added to it. See `Result.empty` for a convenient
41
+ * right unit of chains of `*:`s.
42
+ */
43
+ def *: [U <: Tuple ](other : Result [U , List [E ]]): Result [T *: U , List [E ]] = (r, other) match
44
+ case (Ok (x), Ok (ys)) => Ok (x *: ys)
45
+ case (Ok (_), es : Err [? ]) => es
46
+ case (Err (e), Ok (_)) => Err (e :: Nil )
47
+ case (Err (e), Err (es)) => Err (e :: es)
48
+ end extension
49
+
31
50
/** Similar to `Try`: Convert exceptions raised by `body` to `Err`s.
32
51
*/
33
52
def apply [T ](body : => T ): Result [T , Exception ] =
34
53
try Ok (body)
35
54
catch case ex : Exception => Err (ex)
55
+
56
+ /** Right unit for chains of `*:`s. Returns an `Ok` with an `EmotyTuple` value. */
57
+ def empty : Result [EmptyTuple , Nothing ] = Ok (EmptyTuple )
36
58
end Result
37
59
38
60
/** A prompt for `_.?`. It establishes a boundary to which `_.?` returns */
0 commit comments