@@ -28,11 +28,33 @@ object Result:
2828 case Ok (x) => f(x)
2929 case err : Err [_] => err
3030
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+
3150 /** Similar to `Try`: Convert exceptions raised by `body` to `Err`s.
3251 */
3352 def apply [T ](body : => T ): Result [T , Exception ] =
3453 try Ok (body)
3554 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 )
3658end Result
3759
3860/** A prompt for `_.?`. It establishes a boundary to which `_.?` returns */
0 commit comments