Repository navigation
fix(reflect): make nil slices, typed nil interfaces and map lookups match Go - #546
Merged
Merged
Conversation
…atch Go reflect.Zero and New gave an empty slice rather than a nil one for a slice type, so IsNil was false for the zero value of every slice type. IsNil of an interface Value holding a typed nil, such as a nil slice, was true, although the interface has a dynamic type. MapIndex looked up the key's Interface, which boxes a number, so it did not find a number key that SetMapIndex had stored, and it gave a valid Value for a missing key. The zero value of a slice type is now nil, IsNil of an interface is false when it holds a typed nil, and MapIndex looks the key up as SetMapIndex stores it, giving the zero Value for a missing key. Signed-off-by: Randall C. O'Reilly <oreilly@ucdavis.edu>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Three reflect results differed from Go.
reflect.Zero and New(t).Elem() gave an empty slice for a slice type, so IsNil was false for the zero value of every slice type, and a slice set to Zero was not nil. The zero value of a slice type is now null, as the compiler makes a nil slice.
IsNil of an interface Value holding a typed nil, such as an interface holding a nil slice, was true: isTypedNilValue looked through the interface. An interface with a dynamic type is not nil, so IsNil of an Interface Value is now true only when it holds nothing.
MapIndex looked the key up by key.Interface(), which boxes a number with its type, so it missed number keys, which SetMapIndex and compiled code store unboxed; and for a missing key it gave a valid Value of an invalid type rather than the zero Value. It now looks the key up as SetMapIndex stores it and gives the zero Value for a missing key or a nil map.
reflect_nil_values checks each case against Go.