More sophistication; Allow requiresInit to be specified per-field

This commit is contained in:
Zahary Karadjov
2020-03-27 18:43:23 +02:00
committed by Andreas Rumpf
parent a8b6222c86
commit 7652aede41
4 changed files with 61 additions and 15 deletions

View File

@@ -3,7 +3,9 @@ template accept(x) =
template reject(x) =
static: assert(not compiles(x))
{.experimental: "notnil".}
type
TRefObj = ref object
x: int
@@ -26,6 +28,18 @@ type
else:
discard
PartialRequiresInit = object
a {.requiresInit.}: int
b: string
FullRequiresInit {.requiresInit.} = object
a: int
b: int
FullRequiresInitWithParent {.requiresInit.} = object of THasNotNils
e: int
d: int
TObj = object
case choice: TChoice
of A:
@@ -106,6 +120,23 @@ accept TBaseHasNotNils(a: notNilRef, b: notNilRef, choice: B, indirectNotNils: m
reject TBaseHasNotNils(a: notNilRef, b: notNilRef, choice: B, indirectNotNils: THasNotNilsRef())
accept TBaseHasNotNils(a: notNilRef, b: notNilRef, choice: B, indirectNotNils: THasNotNilsRef(a: notNilRef, b: notNilRef))
# Accept only instances where the `a` field is present
accept PartialRequiresInit(a: 10, b: "x")
accept PartialRequiresInit(a: 20)
reject PartialRequiresInit(b: "x")
reject PartialRequiresInit()
accept FullRequiresInit(a: 10, b: 20)
reject FullRequiresInit(a: 10)
reject FullRequiresInit(b: 20)
accept FullRequiresInitWithParent(a: notNilRef, b: notNilRef, c: notNilRef, e: 10, d: 20)
accept FullRequiresInitWithParent(a: notNilRef, b: notNilRef, c: nil, e: 10, d: 20)
reject FullRequiresInitWithParent(a: notNilRef, b: nil, c: nil, e: 10, d: 20) # b should not be nil
reject FullRequiresInitWithParent(a: notNilRef, b: notNilRef, e: 10, d: 20) # c should not be missing
reject FullRequiresInitWithParent(a: notNilRef, b: notNilRef, c: nil, e: 10) # d should not be missing
reject FullRequiresInitWithParent()
# this will be accepted, because the false outer branch will be taken and the inner A branch
accept TNestedChoices()