-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: reflect: allow Value.Set with zero Value for interface values #52310
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
Comments
It just occurred to me that I could do: vy := reflect.ValueOf(&y).Elem()
vx.Set(vy) // analogous to x = y but that always causes |
This is very similar to #51649 but I think we need to figure out what the scope of "treat zero Value as untyped nil" would be. Where do we stop? If someone can come up with a clear answer for that question, it would help us move forward. |
This proposal has been added to the active column of the proposals project |
In #51649 (comment), I came up with a concrete proposal for comprehensive changes to |
Let's merge this into #51649. Closing as duplicate. |
Consider the following Go code:
Consider the seemingly equivalent Go reflection code:
However, the reflection code about is not quite equivalent since it panics if y is nil.
This occurs because
reflect.ValueOf
loses track of the fact thaty
may be a nil interface valueand simply constructs a zero
reflect.Value
.The workaround is somewhat convoluted and not immediately obvious as necessary:
I propose changing the behavior of
func (v Value) Set(x Value)
such thatx
may be a zeroValue
only ifv
is an interface value, in which case it simply stores a nil value.This should be a generally safe change since switching from a panic to a non-panic is not likely to break existing code.
The text was updated successfully, but these errors were encountered: