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: Go 2: replace if and switch with when keyword #38280

Closed
moviegoer22 opened this issue Apr 6, 2020 · 4 comments
Closed

proposal: Go 2: replace if and switch with when keyword #38280

moviegoer22 opened this issue Apr 6, 2020 · 4 comments
Labels
Milestone

Comments

@moviegoer22
Copy link

moviegoer22 commented Apr 6, 2020

This can consider discussion, just sharing my idea, to replace 2 keywords we can attempt to address decade old problem: if err != nil and switch x:

  1. Focus on less typing
    When there is something in err variable, it will fall under the same condition as default case in switch/when statement. It should sound natural.
when err {
    ... handle error
    return
}
  1. This is an inverted of Ruby case...when statement.
    Expand the scope in place rather than replacing if err != nil with switch.
when err := i.(type) {
case T:
    ...
default:
    ...
}
@ianlancetaylor ianlancetaylor changed the title Replace if and switch with when keyword proposal: Go 2: replace if and switch with when keyword Apr 6, 2020
@gopherbot gopherbot added this to the Proposal milestone Apr 6, 2020
@ianlancetaylor ianlancetaylor added v2 A language change or incompatible library change LanguageChange labels Apr 6, 2020
@ianlancetaylor
Copy link
Contributor

We can't realistically get rid of if and switch, since that would break all existing Go code and documentation. Is it really worth adding a new keyword?

Also, how does else work when using when? Do you always have to write cases? That doesn't seem like an advantage.

@ianlancetaylor
Copy link
Contributor

Per the comments above, and the negative emoji voting, this is a likely decline. Leaving open for four weeks for final comments.

@ianlancetaylor
Copy link
Contributor

No further comments.

@golang golang locked and limited conversation to collaborators May 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants
@ianlancetaylor @gopherbot @moviegoer22 and others