result: `and` can deal with heterogenous values

This commit is contained in:
Jacek Sieka 2020-04-03 13:33:24 +02:00 committed by zah
parent 86739f99c4
commit 9414202d53
2 changed files with 4 additions and 1 deletions

View File

@ -266,7 +266,7 @@ func mapCast*[T0, E0](
if self.isOk: result.ok(cast[T1](self.v)) if self.isOk: result.ok(cast[T1](self.v))
else: result.err(self.e) 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 ## Evaluate `other` iff self.isOk, else return error
## fail-fast - will not evaluate other if a is an error ## fail-fast - will not evaluate other if a is an error
## ##

View File

@ -32,6 +32,9 @@ doAssert (rErr and rOk).isErr
doAssert (rOk or rErr).isOk doAssert (rOk or rErr).isOk
doAssert (rErr or rOk).isOk doAssert (rErr or rOk).isOk
# `and` heterogenous types
doAssert (rOk and rOk.map(proc(x: auto): auto = $x))[] == $(rOk[])
# Exception on access # Exception on access
let va = try: discard rOk.error; false except: true let va = try: discard rOk.error; false except: true
doAssert va, "not an error, should raise" doAssert va, "not an error, should raise"