results: work around nim codegen bug (#192)
https://github.com/nim-lang/Nim/issues/22049
This commit is contained in:
parent
13e55ed27a
commit
6c97f11c7c
|
@ -865,6 +865,10 @@ template tryGet*[E](self: Result[void, E]) = self.tryValue()
|
|||
template unsafeGet*[T: not void, E](self: Result[T, E]): T = self.unsafeValue()
|
||||
template unsafeGet*[E](self: Result[void, E]) = self.unsafeValue()
|
||||
|
||||
# `var` overloads should not be needed but result in invalid codegen (!):
|
||||
# TODO https://github.com/nim-lang/Nim/issues/22049
|
||||
func get*[T: not void, E](self: var Result[T, E]): var T = self.value()
|
||||
|
||||
func get*[T, E](self: Result[T, E], otherwise: T): T {.inline.} =
|
||||
## Fetch value of result if set, or return the value `otherwise`
|
||||
## See `valueOr` for a template version that avoids evaluating `otherwise`
|
||||
|
|
|
@ -482,3 +482,13 @@ block: # Constants
|
|||
proc checkIt(v: WithOpt) =
|
||||
doAssert v.opt.isNone()
|
||||
checkIt(noneWithOpt)
|
||||
|
||||
block: # TODO https://github.com/nim-lang/Nim/issues/22049
|
||||
var v: Result[(seq[int], seq[int]), int]
|
||||
v.ok((@[1], @[2]))
|
||||
let (a, b) = v.get()
|
||||
doAssert a == [1] and b == [2]
|
||||
let (c, d) = v.tryGet()
|
||||
doAssert c == [1] and d == [2]
|
||||
let (e, f) = v.unsafeGet()
|
||||
doAssert e == [1] and f == [2]
|
||||
|
|
Loading…
Reference in New Issue