tnoreturn.nim 712 B

123456789101112131415161718192021222324252627282930313233
  1. block: # issue #22216
  2. type
  3. Result[T, E] = object
  4. case oVal: bool
  5. of false:
  6. eVal: E
  7. of true:
  8. vVal: T
  9. func raiseResultDefect(m: string) {.noreturn, noinline.} =
  10. raise (ref Defect)(msg: m)
  11. template withAssertOk(self: Result, body: untyped): untyped =
  12. case self.oVal
  13. of false:
  14. raiseResultDefect("Trying to access value with err Result")
  15. else:
  16. body
  17. func value[T, E](self: Result[T, E]): T {.inline.} =
  18. withAssertOk(self):
  19. self.vVal
  20. const
  21. x = Result[int, string](oVal: true, vVal: 123)
  22. z = x.value()
  23. let
  24. xx = Result[int, string](oVal: true, vVal: 123)
  25. zz = x.value()
  26. doAssert z == zz