-
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: Go 2: Remove 'new' function #28293
Comments
Altho Go2 won't be strictly backwards-compatible, the principals have said recently that they don't want to break existing code by removing features (see links). That would appear to preclude changes like this. |
This has been proposed before - for example, see #20446. |
I always thought it would make sense to combine |
|
One of the things I love about C that I can't do in C++ and other OOP languages is the ability to name a variable I won't lie, I'm almost in favor of this almost entirely so I can name a variable |
This is also related to #9097. We aren't going to get rid of We're fairly unlikely to get rid of Closing in favor of #9097 and friends. |
That's odd, I remember the compiler complaining when I tried to do that before, maybe it changed. |
This has been thought about before in the community but no one has mentioned it here before.
The new function returns a pointer to the zero value of the type passed into it. For structs, this can be replaced with the syntax
&MyStruct{}
, which has exactly the same result.For maps and slices, the same syntax can be used. Additionally, it is rarer that you will need a pointer to a map or slice than a struct.
For all the other types, new can potentially be used if you want to obtain a concrete pointer to them; however, this use-case seems so narrow that it raises the question of the point of having new at all.
Currently, every instance of the new function in the standard library can be trivially replaced with the literal syntax. It is used in some test files, but these could be replaced with maybe two lines of code.
So, it seems that the only uses of new is for aesthetic reasons, which for me doesn't justify its existence at all. Removing it makes the language easier to learn as it removes the chance of confusion from someone coming from an OOP background.
The text was updated successfully, but these errors were encountered: