Yes, Stefan_Salewski is right:
type
NodeKind = enum # the different node types
nkInt, # a leaf with an integer value
nkFloat # a leaf with a float value
# Version 1 with object variant
Node = ref NodeObj
NodeObj = object
case kind: NodeKind # the ``kind`` field is the discriminator
of nkInt: intVal: int
of nkFloat: floatVal: float
# Version 2 with conditional fields
Node2[NK] = ref NodeObj2[NK]
NodeObj2[NK: static[NodeKind]] = object
when NK == nkInt:
intVal: int
elif NK == nkFloat:
floatVal: float
# Compiles fine
let a = @[NodeObj(kind: nkInt), NodeObj(kind: nkFloat)]
# Doesn't compile
let b = @[NodeObj2[nkInt](), NodeObj2[nkFloat]()]
- Differences: Object variant vs static conditional fields mratsim
- Re: Differences: Object variant vs static conditional... Stefan_Salewski
- Re: Differences: Object variant vs static conditi... Tiberium
- Re: Differences: Object variant vs static con... planhths
- Re: Differences: Object variant vs static... Udiknedormin
- Re: Differences: Object variant vs s... LeuGim
- Re: Differences: Object variant ... mratsim
- Re: Differences: Object vari... LeuGim
- Re: Differences: Object vari... mratsim
- Re: Differences: Object vari... Udiknedormin
