-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] reset()
on variant discriminators
#56
Comments
I like the idea. I was also thinking of supporting the following syntax (based on the example above)
Anyway, case objects needs a couple of improvements. |
Nice solution. Syntactically way easier than what I antipicated. |
@Araq Thanks. |
Nice solution indeed. I like @cooldome's idea too. |
Is there a case when assignment to the discriminator should not be accompanied with |
Not sure what you mean. You want a version of |
"I want" nim to automatically reset the variant object fields upon discriminant change. type Foo = object
case discr: bool
of true:
a: int
of false:
b: float
var f: Foo
f.b = 5.0
d.discr = true # resets implicitly
assert(f.a == 0)
f.a = 5
f.discr = false # resets implicitly
assert(f.b == 0)
f.discr = true # resets implicitly
assert(f.a == 0)
# EDIT:
f.a = 5
f.discr = true # Doesn't reset because discr doesn't change. Don't know if it's good or not though...
assert(f.a == 5) |
Not sure that I would be in favor of hiding too much implicit behavior behind proc `discr=`(f: var Foo; discr: bool) =
reset(f)
f.`discr` = discr Or maybe something with the experimental dot operator feature. |
Instead we implemented #407 which solves more problems as it also addresses the (annoying) restrictions within object constructors. |
According to Araq's post here and the manual here part of the design of of
reset()
is to allow an instance of a variant object to transition from one branch to another, something you cannot do with vanilla (i.e.=
) reassignment of the discriminator by design.In other terms, say
a.kind
is a discriminator in a variant and you first assign it viaa.kind = Mario
, it follows that any subsequent reassignment -a.kind = Luigi
- would raiseFieldError
. To transitiona.kind
fromMario
toLuigi
, one would first need toreset(a)
. This cleans out the entire contents of the object, which then permits the assignmenta.kind = Luigi
.The problem here is the "whole object" part.
reset()
is fine if that whole object is just a single case section, but Nim permits fields outside of case sections, as well as multiple case sections alongside each other, which a simplereset()
cleans out entirely. We need a way to be able to reset a case section whilst preserving the remaining contents of an object.I propose allowing
reset()
on case discriminators. Currently, you cannot callreset()
on them, and will complain of:Calling
reset()
on a case discriminator should do two things:Take for instance, this example:
which is represented in C as:
If
foo
were a variable of typeThingy
, then callingreset(foo.kind)
would internally zero outkind
and the contents ofkindU
.Thoughts?
The text was updated successfully, but these errors were encountered: