From 9414202d53fac99a0b1af33acac816ff9236e6d0 Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Fri, 3 Apr 2020 13:33:24 +0200 Subject: [PATCH] result: `and` can deal with heterogenous values --- stew/result.nim | 2 +- tests/test_result.nim | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) 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"