-
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: spec: drop new(int) in favor of &int{} #20446
Comments
This version would mesh a little better with #12854, too. |
You seem to be suggesting that we permit |
That's correct, but it is also a suggestion to get rid of It may be a comparatively low-value feature, but it's one that I wanted to put forward especially in connection with other proposals relating to compound literals and zero values. |
But getting rid of Every change to the language has benefits and drawbacks. Eliminating To put it another way: Go is now an existing working language that people are actively using. It's not a playground for language design. Breaking existing users is pretty much a non-starter. |
Also you are implying that having two ways of accomplishing something is a problem that needs to be solved — that we should avoid that at all costs. But we can reduce this line of thinking to absurd levels: do we get rid of arrays and slices because we have maps? And even after it makes sense, the "is it worth it" test still doesn't really pass or fail by that much: removing In fact, I can think of a point against it, given Go 1 rules: In order for this proposal to make sense, we would need to expand the composite literal syntax to all types, so we can say |
I am not sure the proposal covers all use cases of new, e.g. if the type to be allocated is determined at the run time. Without it we would have to use reflection and recursion to do so, and in addition the code would break if new builtin types are added to the language. |
@ghasemloo The existing |
Related to #9097. |
This is basically the same idea as #9097. Closing in favor of the earlier issue. |
This is sort of spawning out of #19642, and may be related to its resolution.
Currently, struct types (and, less commonly, array, slice, and map types) can have a pointer to a zero value constructed in two different ways:
new(T)
and&T{}
. I've never seen a style guide recommending one or the other, but the latter is more clear next to&T{foo}
.Having two ways to do something isn't ideal, so one potential solution is to allow composite literals for all types (with one possible exception I'll get to). For any type for which Go 1 does not allow composite literals (numeric, boolean, function, interface, channel, and pointer types), the literal must be empty and yields the zero value. So instead of writing
new(int)
, you would write&int{}
.If this is done, the
new
builtin becomes redundant. We can therefore remove it from the language to simplify. I'm not sure this is necessarily a good idea, but it would remove the duplication.The only issue is pointer types. Under this proposal, I think that
*T{}
is ambiguous between "a pointer to a zeroT
" and "anil
pointer toT
" (I frequently mistype*T{}
for&T{}
). I'm not sure the best resolution for this.The text was updated successfully, but these errors were encountered: