Skip to content
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

reflect: reflect.Value.IsZero #7501

Closed
lukescott opened this issue Mar 10, 2014 · 17 comments

Comments

Projects
None yet
@lukescott
Copy link

commented Mar 10, 2014

Many encoding packages have a custom internal "isEmptyValue" function. The
functionality is pretty much identical. This expands to other packages that use reflect,
such as an ORM (is primary key set?).

It would be useful to have a standard "IsZero" method on reflect.Value,
something like this (copied from encoding/xml):

func (v Value) IsZero() bool {
    switch v.Kind() {
    case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
        return v.Len() == 0
    case reflect.Bool:
        return !v.Bool()
    case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
        return v.Int() == 0
    case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
        return v.Uint() == 0
    case reflect.Float32, reflect.Float64:
        return v.Float() == 0
    case reflect.Interface, reflect.Ptr:
        return v.IsNil()
    }
    return false
}
@bradfitz

This comment has been minimized.

Copy link
Member

commented Mar 10, 2014

Comment 1:

At least for Map and Slice, though, there's a difference between nil and Len 0.
@robpike

This comment has been minimized.

Copy link
Contributor

commented Mar 26, 2014

Comment 2:

I believe this is correct:
func (v Value) IsZero() bool {
    switch v.Kind() {
    case reflect.Array, reflect.String:
        return v.Len() == 0
    case reflect.Bool:
        return !v.Bool()
    case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
        return v.Int() == 0
    case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
        return v.Uint() == 0
    case reflect.Float32, reflect.Float64:
        return v.Float() == 0
    case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
        return v.IsNil()
    }
    return false
}
it seems a reasonable feature request.

Labels changed: added release-go1.4, repo-main.

Status changed to Accepted.

@extemporalgenome

This comment has been minimized.

Copy link

commented May 25, 2014

Comment 3:

r's suggested revision still slightly deviates from the language defined notion of a
type's zero value. Arrays and structs are zero if all their members have respective zero
values. Yes, zero-size types (such as with the above array length check) are guaranteed
to be their own zero value, but that's not a particularly common scenario.
Zero length slices and maps would only be zero if they're also nil.
@rsc

This comment has been minimized.

Copy link
Contributor

commented Sep 16, 2014

Comment 4:

We could define IsZero but it won't be usable in any of the packages that define their
own 'emptiness' predicates.
@lukescott

This comment has been minimized.

Copy link
Author

commented Sep 16, 2014

Comment 5:

Perhaps there could be an "IsZero" and "IsEmpty" method. "IsEmpty" would be the same as
"IsZero", but also include zero length slices and maps.
Including structs would be nice, although the current omitempty implementations in
encoding/* don't. However, that doesn't mean those implementations have to use "IsEmpty"
for Go 1. Third party / internal encoding libraries may choose to use the method though,
which still makes it useful to implement.
@rsc

This comment has been minimized.

Copy link
Contributor

commented Sep 29, 2014

Comment 6:

Maybe for 1.5.

Labels changed: added release-go1.5, removed release-go1.4.

@bradfitz bradfitz modified the milestone: Go1.5 Dec 16, 2014

@bradfitz bradfitz removed the release-go1.5 label Dec 16, 2014

@rsc rsc removed accepted labels Apr 14, 2015

@bradfitz bradfitz modified the milestones: Go1.6, Go1.5 Jul 7, 2015

@rsc

This comment has been minimized.

Copy link
Contributor

commented Oct 16, 2015

It's not clear to me what would use IsZero if we added it.
It's easy for packages to define what they really want themselves.
I'm wary of continuing to add to reflect.
I don't want it to be a kitchen sink of helpers.

@hraban

This comment has been minimized.

Copy link

commented Oct 26, 2015

Not arguing for or against, just a datapoint on intuitiveness: I found this page while looking for a reflect.IsZero that would actually test for zero values of structs.

reflect.IsZero(x) feels like it should return true for any y in var x y, to me.

But this is moot if it's not added.

@rsc rsc modified the milestones: Go1.6Maybe, Go1.6, Go1.7 Nov 25, 2015

@rsc

This comment has been minimized.

Copy link
Contributor

commented Nov 25, 2015

Postponing to Go 1.7 (Go1.6Maybe was a misclick), along with discussion of omitzero in xml and json.

@gopherbot

This comment has been minimized.

Copy link

commented May 12, 2016

CL https://golang.org/cl/23064 mentions this issue.

@bradfitz bradfitz modified the milestones: Go1.8, Go1.7 May 12, 2016

@rsc rsc added this to the Go1.9Early milestone Oct 18, 2016

@ereyue

This comment has been minimized.

Copy link

commented Feb 24, 2019

Any updates on this topic? This should be a reasonable feature request.

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Feb 25, 2019

I'll turn this into a proposal for consideration.

@ianlancetaylor ianlancetaylor changed the title reflect: feature request: IsZero method proposal: reflect: reflect.Value.IsZero Feb 25, 2019

@ianlancetaylor ianlancetaylor modified the milestones: Unplanned, Proposal Feb 25, 2019

@josharian

This comment has been minimized.

Copy link
Contributor

commented Feb 25, 2019

Note that there’s also discussion of an internal runtime.isZero for optimized codegen:
#23929

@andybons

This comment has been minimized.

Copy link
Member

commented Mar 27, 2019

Proposal tentatively approved. @rsc any objections?

@andybons andybons changed the title proposal: reflect: reflect.Value.IsZero reflect: reflect.Value.IsZero Mar 27, 2019

@ianlancetaylor ianlancetaylor modified the milestones: Proposal, Unplanned Mar 27, 2019

@gopherbot

This comment has been minimized.

Copy link

commented Apr 10, 2019

Change https://golang.org/cl/171337 mentions this issue: reflect: implement Value.IsZero()

@gopherbot gopherbot closed this in c40bffd Apr 13, 2019

@go101

This comment has been minimized.

Copy link

commented Apr 13, 2019

So, this will be available in Go 1.13?

@andybons

This comment has been minimized.

Copy link
Member

commented Apr 13, 2019

@go101 yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.