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: cmd/gofmt: allow `case` clause to be on the same line #38795

Closed
tommyknows opened this issue May 1, 2020 · 2 comments
Closed

proposal: cmd/gofmt: allow `case` clause to be on the same line #38795

tommyknows opened this issue May 1, 2020 · 2 comments
Labels
Milestone

Comments

@tommyknows
Copy link

@tommyknows tommyknows commented May 1, 2020

What did you do?

Writing switch statements with single line cases, like so:

package main

import "fmt"

func main() {
        x := "this"

        switch x {
        case "this":  fmt.Println("yes!")
        case "that":  fmt.Println("no...")
        case "thiis": fmt.Println("almost")
        default:      fmt.Println("not at all")
        }
}

What did you expect to see?

it would be nice to see that gofmt does not put all the fmt.Println statements on a newline (leaving the above example as is).

The rule should be that if and only if all case clause bodies are one-liners, the user is allowed to put them on the same line as the case keyword.

Also, similar to other formatting cases, if the user already puts the code on the next line, leave it there.

Additionally, as can be seen in the example, align the start of every clause body, similarly to end of line comments over multiple lines.

i think it improves readability (iff there is only one line) and also looks similar to pattern matching from other languages like Rust or Haskell (which I consider to be syntactically nice-looking).

What did you see instead?

package main

import "fmt"

func main() {
        x := "this"

        switch x {
        case "this":
                fmt.Println("yes!")
        case "that":
                fmt.Println("no...")
        case "thiis":
                fmt.Println("almost")
        default:
                fmt.Println("not at all")
        }
}

Additional notes

The Go compiler would not be affected by this, as the scanning does not necessarily expect a newline (one can compile the first example just fine).
It is also backwards-compatible and actually not changing code that's already written.

Also, not sure if this should be a proposal or not. Let me know if it should be.

@mvdan
Copy link
Member

@mvdan mvdan commented May 1, 2020

We don't allow if cond { stmt }, so I don't see why switch cases should be any different. The current format is perhaps a bit verbose, but it's consistent.

@andybons andybons changed the title cmd/gofmt: allow `case` clause to be on the same line proposal: cmd/gofmt: allow `case` clause to be on the same line May 1, 2020
@gopherbot gopherbot added this to the Proposal milestone May 1, 2020
@gopherbot gopherbot added the Proposal label May 1, 2020
@tommyknows
Copy link
Author

@tommyknows tommyknows commented May 2, 2020

I agree, but I also see case statements different to if cond { stmt }, as they're not surrounded by curly braces. I also use switch mostly for some kind of pattern matching, which would make the similarity to other languages nice.

However, judging by the reactions on the posts, I can see that people are not really enthusiastic on this change. I'll give this another week for other comments, and then will close it.

@tommyknows tommyknows closed this May 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.