@@ -28,6 +28,24 @@ 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
+
31
49
/** Simlar to `Try`: Convert exceptions raised by `body` to `Err`s.
32
50
* In principle, `Try[T]` should be equivalent to `Result[T, Exception]`.
33
51
* Note that we do not want to catch and reify all Throwables.
@@ -37,9 +55,12 @@ object Result:
37
55
* (Generally, the focus on `Throwable` in Scala libraries is a mistake.
38
56
* Use `Exception` instead, as it was meant to in Java.)
39
57
*/
40
- def apply [T ](body : => T ): Result [T , Exception ] =
58
+ inline def apply [T ](body : => T ): Result [T , Exception ] =
41
59
try Ok (body)
42
60
catch case ex : Exception => Err (ex)
61
+
62
+ /** Right unit for chains of `*:`s. Returns an `Ok` with an `EmotyTuple` value. */
63
+ def empty : Result [EmptyTuple , Nothing ] = Ok (EmptyTuple )
43
64
end Result
44
65
45
66
/** A prompt for `_.?`. It establishes a boundary to which `_.?` returns */
0 commit comments