-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: reflect: add method (Value) IsEmpty() #59451
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
Change https://go.dev/cl/482415 mentions this issue: |
cc @dsnet |
I'm not sure these are great examples of why this should be added in
|
Thank you. However, it is strange that there are dozens of lines of identical code in the two packages. Do we currently need to put this into an internal public package? |
What is the precise definition of |
Thanks. IsEmpty reports whether v is an empty value for its type. When v is of type int/uint/float/bool/string, its processing logic is completely consistent with IsZero. But when v is array/slice/map, if the length is 0, it will return true, without calling IsNil |
Channels have a length. Is empty the same as nil or same as the length? I think nil, but maybe it’s controversial. |
Thanks, and I know that I'm being pedantic, but that can't be the right definition. The implementation of These details matter if we're going to add a new exported method to the standard library. It has to have a clear definition and clear purpose apart from any use in the json or xml packages. Looking further at the encoding/json package, I see that the definition there, for the "omitempty" option, is "false, 0, a nil pointer, a nil interface value, and any empty array, slice, map, or string." I don't think that's useful enough to add to the reflect package. |
I searched other files of the standard library, and there is no other similar use except these two places. It is indeed a bit of a luxury to add a new method in the standard library, because the frequency of use is not enough. Thank you for your patience and reply. |
Closing this proposal: Doesn't seem necessary. Thanks to everyone who participated in the discussion. |
Recently, I am working on a project that frequently uses reflection, and there are many scenarios where I need to determine whether the reflect.Value is empty (different from the existing IsZero).
I searched the source code and found similar private methods with redundancy scattered across two packages:
https://github.com/golang/go/blob/master/src/encoding/json/encode.go#L303
https://github.com/golang/go/blob/master/src/encoding/xml/marshal.go#L1118
I would like to discuss whether it is possible to add a new method
IsEmpty
to the reflect package, which eliminates the need for duplicate definitions in individual projects and simplifies existing source code.Just like this https://go-review.googlesource.com/c/go/+/482415
The text was updated successfully, but these errors were encountered: