-
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 ++ and -- operators #21263
Comments
I don't think the stated benefits are anywhere near convincing enough to pay off for breaking every single Go program in existence (even if go-fix-able). Go 2 doesn't have the luxury of starting from a clean slate where these ideas might be more appropriate. Also, this is not Python. |
I'd miss them in simple counting for loops, just for the reduced line noise. Maybe it's a good idea anyway, but I have a hard time convincing myself that this could be among the top three to five significant language changes that Russ indicated are budgeted for Go 2. |
Yeah, I need to write up an experience report about loops one of these days: I'm not convinced that we should keep the three-slot
Agreed. It would have to be one of the minor changes, which I think also implies that it couldn't be the only one that requires a |
It's easy to propose additions to the language, but that's how you end up with a complex behemoth like C++. Where I'm going with this proposal (and #21165) is that I think it's important that we also consider which parts of the language aren't pulling their weight: otherwise, Go 2 with be strictly more complex than Go 1. (#20446 ("drop |
It would be useful to have some statistics (say, over the std lib) about their use relative to loops and perhaps other operators. |
@griesemer I am not aware of any other features in Go that exist to save a handful of characters per operation. What is different about these operators that makes them exceptional? The two closest examples I can think of are:
Agreed. In particular, I suspect that we could make most existing uses of |
https://github.com/josharian/gofor has a crude script and some four-year-old data in the readme. I doubt usage patterns have changed substantially since. I hacked it together long ago in preparation to write a proposal to expand range loops to work with integer ranges. (I ended up not writing it because I perceived it to be pointless, in light of some of Rob's comments on golang-nuts.) |
@bcmills As I said, they are here plainly for convenience (the convenience being that they are shorter and easier to type than the equivalent). When we started with Go there was an (perhaps incorrect) understanding that |
I find "they're confusing to newcomers" a weak argument. The argument is backed by a link to an FAQ entry that suggests that our operators are confusing because they're less powerful than those in C. Based on that argument, a valid fix would be to make them more complex. In reality, I don't think that these operators are confusing. People with no solid background in C-like languages can easily learn our definition of the operator, and people who do have said background are more than capable of relearning a small detail in a matter of minutes. You're correct in pointing out that our version of the operator doesn't have any benefit over using Both Rust and Swift started with C-like operators and decided to remove them entirely, as opposed to adopting our definition. Going from very confusing operator to no operator has much higher payoffs than going from already simple operator to no operator. |
From the Rust FAQ:
The stated reason for avoiding the inclusion in Rust was confusing evaluation order (this seems to apply also for Swift); an issue which Go does not face as increment and decrement statements are not expressions. I understand my comment is mostly noise, as @dominikh has already pointed this out. Just tried to frame the wording in a different way. |
This change is the single stupidest thing Swift did. Let's not make the same mistake. It's a shame that the way they work in C has caused people to consider them harmful. But I don't buy the "it's too confusing for newcomers" argument, because a) Go makes them sufficiently different enough, and b) it's incredibly patronizing to assume that something as simple as "this increases x by 1" is too confusing to beginners. I know it's hard for us to do, but we need to not assume that beginners are dumb, and give them some show of confidence. After all, people who use the |
Which of these look most confusing to newcomers?
I think the |
Honestly? |
Despite using these regularly I’d be ok with writing out x = x + 1.
These operators are one more thing for new programmers to learn. We don't need them. |
The benefits are not worth the drawbacks. While there are supporters, there are more detractors. Closing. |
The
--
and++
operators (called IncDec statements in the spec) are confusing for newcomers to the language. In other languages with similar syntax,++
and--
can appear as either prefix or postfix expressions. However, in Go they can only be used in the postfix position and are statements, not expressions.In that role, they are completely redundant:
+= 1
and-= 1
are statements with exactly the same semantics. Perhaps more importantly,+=
and-=
in Go match the semantics of similar operators in other languages.For comparison:
The text was updated successfully, but these errors were encountered: