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: support switch expressions #36036

Closed
auula opened this issue Dec 7, 2019 · 2 comments
Closed

proposal: Go 2: support switch expressions #36036

auula opened this issue Dec 7, 2019 · 2 comments
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
Milestone

Comments

@auula
Copy link

auula commented Dec 7, 2019

1.My proposals and needs:

  • I hope that the switch statement in the new version of go language will be as easy to use as the switch language in Java 13 version.Thank you for hoping that the official will accept my opinion!

  • This is how we currently write switch statements:

package main

import "fmt"

var (
	marks = 90
	grade = "D"
)

func main() {
	switch marks {
	case 90:
		grade = "A"
	case 80:
		grade = "B"
	case 50, 60, 70:
		grade = "C"
	default:
		grade = "D"
	}
	fmt.Println("Your grade is:", grade)
}
  • I suggest designing a simplified or more advanced switch statement:
       <T> grade = switch (marks) {
    	        case 90 -> "A"
		case 80 -> "B"
		..... 
    	        default -> "D"
	};
       fmt.Println("Your grade is:", grade)
  • 👆 <T> Is generic & I think it can also support function return💡
       <T> grade = switch (marks,arg[]) {
    	        case 90 -> func (int arg[]) int grade{
			return arg + 90;
		}
		case 80 -> func (int arg[]) int grade{
			return arg + 80;
		}
		..... 
    	        default -> ....  
	};
	fmt.Println("Your grade is:", grade)

2.The version of go I currently use is:

go version go1.13 darwin/amd64

3.What did you expect to see?

  • I hope this new version has this new feature ~~

4.Of course, these are my personal needs and opinions, and do not represent everyone's thoughts. I just have this idea. I hope you can consider it, thank you😄🤝~

@auula auula closed this as completed Dec 7, 2019
@auula auula reopened this Dec 7, 2019
@auula auula changed the title Requirements for "switch" statements🙏 Requirements for "switch" statements🙏 label:FeatureRequest Dec 7, 2019
@golang golang deleted a comment Dec 7, 2019
@ianlancetaylor ianlancetaylor changed the title Requirements for "switch" statements🙏 label:FeatureRequest proposal: Go 2: support switch expressions Dec 7, 2019
@gopherbot gopherbot added this to the Proposal milestone Dec 7, 2019
@ianlancetaylor ianlancetaylor added v2 A language change or incompatible library change LanguageChange labels Dec 7, 2019
@ianlancetaylor
Copy link
Contributor

Go is a statement oriented language. It does not currently support any expressions that contain blocks. It would be odd to permit switch expressions and not also permit if expressions, or select expressions, or perhaps even for expressions.

Also, in my opinion, supporting complex expressions that contain blocks does not make a program easier to read and understand. What is the corresponding benefit that is worth that cost in readability?

@auula
Copy link
Author

auula commented Dec 7, 2019

Thank you ☺️

@auula auula closed this as completed Dec 7, 2019
@golang golang locked and limited conversation to collaborators Dec 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
Projects
None yet
Development

No branches or pull requests

3 participants