results: work around nim codegen bug (#192)

https://github.com/nim-lang/Nim/issues/22049
This commit is contained in:
Jacek Sieka 2023-06-08 17:24:19 +02:00 committed by GitHub
parent 13e55ed27a
commit 6c97f11c7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 0 deletions

View File

@ -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`

View File

@ -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]