That could work. I would be concerned about something though: will binary zero
always mean that it is safe to change branches? It is possible that a varient
may be perfectly valid as zero, even when initialized. An example could explain
better:
type
Variant = enum
Poi
Another solution is to allow object case transitions when the object consists
of binary zeros which is what `reset` does. `reset` is documented to enable
object case transitions. This would not require a hidden `wasInitialized` bool.
@twetzel59, there is extra warning you can enable that you can find useful:
warnProveField
Just as an idea, the flexible solution would be to able set discriminant if you
provide the new value simultaneously with discriminant and reject at compile
time otherwise.
Something like:
var t = Thing(kind: Zeroth, f: 3.14)
t.kind = First # not compiles
t.i = 12 # fa
It is reasonably easy to fix but adds 1 byte to case objects. The cause is that
code like `n.kind = First; t.i = 8` needs to work, so transitions from the
"zero" state have be be allowed. Alternatively we can require full object
constructions and disallow all assignments to the discriminator. Th
There seems to be a bug, however, by which this check doesn't catch such an
error, even in debug mode.
For example, try the following example.
type
Kind = enum
Zeroth,
First,
Thing = object
case kind: Kind
of Zeroth:
f: fl
IMO, Nim forbids assignment to discriminant fields anyway. Check is disabled in
the release mode, but it is on by default in debug.
One of the things that I currently find very concerning about object variants
is how they can introduce runtime errors when the discriminator is assigned to
in an incorrect manner. As it turns out, there [seems to be a compiler
bug](https://github.com/nim-lang/Nim/issues/6918) that causes not on