From 8a1b6ab4dca1f2075fec671659de8709a127f722 Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Fri, 11 Nov 2022 14:26:59 +0100 Subject: [PATCH] results: work around void member codegen issue (#150) --- stew/results.nim | 34 +++++++++++++++++++++++++++++----- tests/test_results.nim | 12 ++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/stew/results.nim b/stew/results.nim index f758eed..9de9560 100644 --- a/stew/results.nim +++ b/stew/results.nim @@ -304,11 +304,35 @@ type ## https://github.com/nim-lang/Nim/issues/13879 - double-zero-init slow ## https://github.com/nim-lang/Nim/issues/14318 - generic error raises pragma - case o: bool - of false: - e: E - of true: - v: T + # TODO https://github.com/nim-lang/Nim/issues/20699 + # case o: bool + # of false: + # e: E + # of true: + # v: T + + when T is void: + when E is void: + o: bool + else: + case o: bool + of false: + e: E + of true: + discard + else: + when E is void: + case o: bool + of false: + discard + of true: + v: T + else: + case o: bool + of false: + e: E + of true: + v: T Opt*[T] = Result[T, void] diff --git a/tests/test_results.nim b/tests/test_results.nim index 857b881..255e626 100644 --- a/tests/test_results.nim +++ b/tests/test_results.nim @@ -403,3 +403,15 @@ block: # Experiments counter2 += 1 doAssert counter2 == 1, "one-item collection when set" + +block: # Constants + # TODO https://github.com/nim-lang/Nim/issues/20699 + type + WithOpt = object + opt: Opt[int] + const + noneWithOpt = + WithOpt(opt: Opt.none(int)) + proc checkIt(v: WithOpt) = + doAssert v.opt.isNone() + checkIt(noneWithOpt)