mopensymimport1.nim 800 B

1234567891011121314151617181920212223242526272829303132333435
  1. type
  2. Result*[T, E] = object
  3. when T is void:
  4. when E is void:
  5. oResultPrivate*: bool
  6. else:
  7. case oResultPrivate*: bool
  8. of false:
  9. eResultPrivate*: E
  10. of true:
  11. discard
  12. else:
  13. when E is void:
  14. case oResultPrivate*: bool
  15. of false:
  16. discard
  17. of true:
  18. vResultPrivate*: T
  19. else:
  20. case oResultPrivate*: bool
  21. of false:
  22. eResultPrivate*: E
  23. of true:
  24. vResultPrivate*: T
  25. template valueOr*[T: not void, E](self: Result[T, E], def: untyped): untyped =
  26. let s = (self) # TODO avoid copy
  27. case s.oResultPrivate
  28. of true:
  29. s.vResultPrivate
  30. of false:
  31. when E isnot void:
  32. template error: untyped {.used, inject.} = s.eResultPrivate
  33. def