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

proposal: builtins: add zero #52307

Closed
earthboundkid opened this issue Apr 12, 2022 · 2 comments
Closed

proposal: builtins: add zero #52307

earthboundkid opened this issue Apr 12, 2022 · 2 comments

Comments

@earthboundkid
Copy link
Contributor

Every individual type can be compared to its zero value (func() == nil, for example), but there is no way to write this code today without using package reflect:

func IsZero[T any](t T) bool {
  return t == zero
}

It must instead be:

func IsZero[T any](t T) bool {
  return reflect.ValueOf(&t).Elem().IsZero()
}

I propose adding a built in constant zero that can be tested for equality with any type.

It could also be returned, as in

if err != nil {
  return zero, err
}
@gopherbot gopherbot added this to the Proposal milestone Apr 12, 2022
@seankhliao
Copy link
Member

Duplicate of #35966

@seankhliao seankhliao marked this as a duplicate of #35966 Apr 12, 2022
@earthboundkid
Copy link
Contributor Author

I searched Github for "universal zero" and that issue did not come up. :-/

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants