-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: builtin: zero function #60695
Comments
Closely related to #35966, which has been duplicated a few times. I don't think a built-in function specifically rather than a |
Of course it's easy to write this yourself. func Zero[T any]() T {
var v T
return v
} Not clear that this comes up often enough to add a built-in language function. |
This proposal immediately reminds me of the "standards" XKCD comic https://xkcd.com/927/, just replace standard with canonical. |
Couldn't you say the same thing about
Personally I've seen Zero Values come up often enough, they are a fundamental concept in the language. |
For those that value readability, I don't think there's any disagreement that |
I've used the |
I think this issue is a duplicate and should be closed. There is a slight difference between a built-in universal zero value and a built-in universal zero function, but the difference can be hashed out in #35966. FWIW, I think a function is strictly inferior to a value because it can't be used for #26842. |
|
Anecdotally, I personally have written variants on I do of course accept that I would personally prefer a predeclared identifier similar to |
This is often used In generic cases |
But still generic code will always stay a niche compared to the mass of Go code. I don't think that the use in generic code would be enough to justify a new builtin. |
More so as the introduction of Generics more or less immediately introduced the |
|
@Nasfame I don't think I've yet needed a generic "value of arbitrary type is the zero value of that type" test, but I can see how that might be useful for some things, such as if you're serializing a value and want to skip serializing anything that would just be the zero value anyway, since it's fine to just leave that unpopulated when deserializing. I think your function func IsZero[T any](v T) bool {
var zero T
return v == zero
} The above is equivalent to comparing I'll concede that |
When |
For those who didn't see the other proposal: So what @carlmjohnson means, I assume, is that I assume that |
|
Thanks for the discussion here. I filed #61372 to try to move a concrete plan forward. In terms of that proposal, you can write |
This proposal is a duplicate of a previously discussed proposal, as noted above, |
Makes sense, thanks @rsc ! |
Presently the language offers a few ways to get the Zero Value of a type, but it would make code more readable to have a canonical way to accomplish this.
This proposal would like to add a new builtin function:
func zero(Type) Type
This is quite similar to the
new
builtin, but returns an instance of Type instead of a Pointer-To-Type.This would serve as shorthand for some of the many ways to get a zero value. For example one could do:
Instead of:
Or instead of:
It also makes comparison with zero values rather concise and self-documenting:
This is more readable than either of these existing tests:
And of course, just like
new
this would play nicely with generics:The text was updated successfully, but these errors were encountered: