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
Slice element types are 'masked' when reflect.Value.Index is called on slices of interfaces #39096
Comments
you are doing 2 different things
this is not limited to reflect |
Hm - very interesting, thanks for the prompt response ! Would you not agree, though, that when the documented behavior is: "Index returns the i'th element of v", If not, could you point to a use case that breaks, if Index had returned such equivalence ? Thanks again for your time - much appreciated. |
i expect the result to be the same as :
which is an example that breaks: you want to set the i'th element to something else, if the type didn't match, with your proposal it wouldn't work |
Yup, that did it. Thanks again ! |
for the record, please refer to the forums for Questions |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
The following code blocks yield different results, and I believe that this inconsistency might not have been intentional (and could be a potential source for bugs).
When iterating on a slice of interfaces (say, the empty interface - this applies to every interface though), passing each value to reflect.ValueOf,
and getting the type / kind of the returned reflect.Value - each value conserves the concrete type, for instance:
So far - this is aligned with my expectations.
However, if I would have been given a reflect.Value of that same slice, and then have iterated it via reflect.Value.Index,
the type of each returned element is set to the element type from the slice header - (e.g. interface{}), and its concrete type and kind are 'masked'.
The above is solved by a simple .Elem() on the returned reflect.Value.
This 'dereferncing', however, is not required when operating in the same manner, but on a slice of concrete types:
What did you expect to see?
Given that:
a. Value.Index(...) says that
Index returns the i'th element
(and not a pointer / wrapper to it),b. There's an inconsistent behavior between using reflect.ValueOf on each element, and using reflect.Value.Index on the []interface{}, and
c. The type of reflect.Value.Index(..), when ran on, say, []int - returns a Value with Kind == Int, and does not require the .Elem() 'dereferencing'
I think that the above is proving to be quite confusing for the user, and that it might have been better to:
To put it in different words, I expect that iterating with reflect.Value.Index, and iterating with reflect.ValueOf on each element, would prove to have a consistent behavior, no matter the type passed in.
What did you see instead?
The resulted types are instead 'masked', and need to be unwrapped via .Elem(), but only when operating on slices of interfaces.
The text was updated successfully, but these errors were encountered: