f
Size: a a a
f
f
M(
f
f
while i < s.len:
var key: string
parseHook(s, i, key)
eatChar(s, i, ':')
when compiles(renameHook(v, key)):
renameHook(v, key)
if key == v.discriminatorFieldName:
var discriminator: type(v.discriminatorField)
parseHook(s, i, discriminator)
new(v, discriminator)
when compiles(newHook(v)):
newHook(v)
break
skipValue(s, i)
if i < s.len and s[i] == '}':
error("No discriminator field.", i)
f
M(
f
f
M(
f
M(
f
f
f
type
Requires {.requiresinit.} = object
Obj = object
case kind: bool
of true:
req: Requires
of false: discard
func build(inKind: bool): Obj =
# I need to init `Obj` with a runtime value for a discriminant, but it is not possible
# because
result = Obj(kind: inKind) # Error: The Obj type requires the following fields to be initialized: req.
result = Obj(kind: inKind, req: default(Requires)) # Error: cannot prove that it's safe to initialize 'req' with the runtime value for the discriminator 'kind'
# I'm cornered by 'requires init' and 'cannot change discriminant', so I need hack around this
# limitation using ... good old `setKind`, which does not
setKind(result, kind, inKind)
if result.kind == true:
result.req = default(Requires)
# Which is possible on a C backend, not on other ones.
f
f
{.push fieldChecks:off.}
func build(inKind: bool): Obj =
result = Obj(kind: true, req: default(Requires))
{.cast(uncheckedAssign).}:
result.kind = inKind
{.pop.}
f
f
import {.all.}
добавили