diff --git a/stew/result.nim b/stew/result.nim index 6d95eb3..9e04c6f 100644 --- a/stew/result.nim +++ b/stew/result.nim @@ -266,7 +266,7 @@ func mapCast*[T0, E0]( if self.isOk: result.ok(cast[T1](self.v)) else: result.err(self.e) -template `and`*[T, E](self, other: Result[T, E]): Result[T, E] = +template `and`*[T0, E, T1](self: Result[T0, E], other: Result[T1, E]): Result[T1, E] = ## Evaluate `other` iff self.isOk, else return error ## fail-fast - will not evaluate other if a is an error ## diff --git a/tests/test_result.nim b/tests/test_result.nim index d9a8d03..40706c6 100644 --- a/tests/test_result.nim +++ b/tests/test_result.nim @@ -32,6 +32,9 @@ doAssert (rErr and rOk).isErr doAssert (rOk or rErr).isOk doAssert (rErr or rOk).isOk +# `and` heterogenous types +doAssert (rOk and rOk.map(proc(x: auto): auto = $x))[] == $(rOk[]) + # Exception on access let va = try: discard rOk.error; false except: true doAssert va, "not an error, should raise"