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: Short statement in switch #56115

Closed
xbt573 opened this issue Oct 9, 2022 · 4 comments
Closed

proposal: Go 2: Short statement in switch #56115

xbt573 opened this issue Oct 9, 2022 · 4 comments

Comments

@xbt573
Copy link

xbt573 commented Oct 9, 2022

Author background

  • Would you consider yourself a novice, intermediate, or experienced Go programmer?
    Something between novice and intermediate
  • What other languages do you have experience with?
    JS/TS, C#, Python

Related proposals

  • Has this idea, or one like it, been proposed before?
    No

  • Does this affect error handling?
    No

  • Is this about generics?
    No

Proposal

  • What is the proposed change?
    This proposal adds short statement in switch, like in if statement
  • Who does this proposal help, and why?
    This proposal helps make switch code more shorter and nicer(?)
  • Please describe as precisely as possible the change to the language.
    Switch now can include short statement, like in if, it's looks like this
switch val := returnSomeInt(); val {
    case 5:
        // do some stuff
}

Or like this, if there is no condition

switch val := returnSomeInt() {
    case val == 5:
        // do some stuff
}
  • What would change in the language spec?
    Note about this short statement in switch

  • Is this change backward compatible?
    Yes

  • Show example code before and after the change.

  • Before

import (
    "rand"
    "time"
    "fmt"
)

func returnSomeInt() int {
    rand.Seed(time.Now().UnixNano())
    return rand.Intn(2)
}

func main() {
    val := returnSomeInt()
    switch val {
        case 0:
            fmt.Println("0!")

        case 1:
            fmt.Println("1!")
    }
}
  • After
import (
    "rand"
    "time"
    "fmt"
)

func returnSomeInt() int {
    rand.Seed(time.Now().UnixNano())
    return rand.Intn(2)
}

func main() {
    switch val := returnSomeInt(); val {
        case 0:
            fmt.Println("0!")

        case 1:
            fmt.Println("1!")
    }
}
  • Orthogonality: how does this change interact or overlap with existing features?
    It's new feature

  • Is the goal of this change a performance improvement?
    No

Costs

  • Would this change make Go easier or harder to learn, and why?
    No

  • What is the cost of this proposal? (Every language change has a cost).
    Adding this thing to language?

  • How many tools (such as vet, gopls, gofmt, goimports, etc.) would be affected?
    gopls, vet(?), gofmt(?)

  • What is the compile time cost?
    Zero

  • What is the run time cost?
    Zero

  • Can you describe a possible implementation?
    No

  • Do you have a prototype? (This is not required.)
    No

@gopherbot gopherbot added this to the Proposal milestone Oct 9, 2022
@zephyrtronium
Copy link
Contributor

The first form you propose already works: https://go.dev/play/p/avLc8LRUPu5

@xbt573
Copy link
Author

xbt573 commented Oct 9, 2022

Oh, really :D
It mentioned in specification, but not in Go tour, i missed it
Anyway, second form is not working, so i think this proposal makes sense

@xbt573
Copy link
Author

xbt573 commented Oct 9, 2022

Second form is working too, but with a required semicolon

package main

import "fmt"

func main() {
	switch x := 1; {
	case x == 1:
		fmt.Println("yes")
	}
}

https://go.dev/play/p/3hM5pzBdALh

@mvdan
Copy link
Member

mvdan commented Oct 9, 2022

See https://go.dev/ref/spec#Switch_statements. I'm going to close this for now, as expression switch statements already support these simple statements as pointed out above. If you want to file a different proposal, feel free to open a new one, but please read the spec carefully first.

@mvdan mvdan closed this as not planned Won't fix, can't repro, duplicate, stale Oct 9, 2022
@golang golang locked and limited conversation to collaborators Oct 9, 2023
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

4 participants