Skip to content

proposal: Go 2: Inline switch statement #61091

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

Closed
godalming123 opened this issue Jun 30, 2023 · 1 comment
Closed

proposal: Go 2: Inline switch statement #61091

godalming123 opened this issue Jun 30, 2023 · 1 comment

Comments

@godalming123
Copy link

Author background

  • Experience in go:
    I am a novice go programmer but know lots of other languages such as
  • Experience in other languages
    • C - Maintain a 2K+ C code project

Related proposals

Proposal

  • What is the proposed change?
    Add a way to return data in a switch statement to assign a variable or pass to a function argument
  • Who does this proposal help, and why?
    It helps right slightly cleaner code for example (example syntax of this feature):
age := 42
ageDescription := switch age {
  case 0...5: "baby"
  case 6...18: "teenager"
  case 18...70: "adult"
  default: "elderly"
}

Is better then assigning an age var and ageDescription var, and then setting ageDescription in each case in the switch. This can become much worse if you are calling a function (example of what this would be like in old go):

someVar = 56
switch someVar {
  case 0..10:
    someFunc("very very long argument A", "very very long argument B", "very very long argument C", "very very long argument D", "your value that changes to A")
   case 10..25:
     someFunc("very very long argument A", "very very long argument B", "very very long argument C", "very very long argument D", "your value that changes to B")
 case 10..25:
     someFunc("very very long argument A", "very very long argument B", "very very long argument C", "very very long argument D", "your value that changes to C")
}
You can quickly see how this becomes hard to manage.
  • Please describe as precisely as possible the change to the language.
    When the switch is used as a token and their is a token at the end of its value, then return that token and if their is no token then return an error when compiling.
  • What would change in the language spec?
    Add a header on inline switches within the switch statements header that describes how they work, and in the code example add the switch statement example as seen above, or something similar.
  • Please also describe the change informally, as in a class teaching Go.
    Switches can also return values like functions, to do this simple add a value at the end of each case statement to be returned.
  • Is this change backward compatible?
    Yes.
  • Orthogonality: how does this change interact or overlap with existing features?
    As I am still learning go I do not know many advanced futures, but from what I do know I don't think that it does interact with any.

Costs

  • Would this change make Go easier or harder to learn, and why?
    If anything easier, because some code can become cleaner.
  • What is the cost of this proposal? (Every language change has a cost).
    I can't think of any other then a little bit more complexity in the go compiler.
  • How many tools (such as vet, gopls, gofmt, goimports, etc.) would be affected?
    gofmt may have to format this but I expect that it would be a simple change and no other tools are affected.
  • What is the compile time cost?
    Any slowdown should not be perceivable compared to random error.
  • What is the run time cost?
    I don't think that their is a runtime cost.
@gopherbot gopherbot added this to the Proposal milestone Jun 30, 2023
@seankhliao
Copy link
Member

Duplicate of #44022

@seankhliao seankhliao marked this as a duplicate of #44022 Jun 30, 2023
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jun 30, 2023
@golang golang locked and limited conversation to collaborators Jun 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants