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: spec: add case-ranges to the expression switch statement #21218
Comments
What makes a type Rangeable? How does that work? |
While reading your comment, I figured out that this term is confusing. so I've changed the proposal. I'm expecting that a case-ranges like this: |
So I get that you want to have syntax like this:
Do What happens if |
In my opinion, the variadic parameter syntax is discordant as an infix operator. I understand that "..." is used within the metasyntax notation which defines Go for the exact purpose of describing ranges, but within the language it acts as a value handling modifier and lacks a harmony with it's use as a simple range indicator. I have no strong feelings about an alternative, but would suggest: |
@daved A new token would be a bit confusing and possibly compatibility-breaking, now people have to distinguish |
@fuzxxl A "range OR variadic" operator/special syntax would be sloppy and could muddle communication. They should be different, because they have fundamentally different meanings despite the small logical similarity. Please, consider what the variadic parameter operator is doing relative to a basic indication of range. |
no need to be constants. switch y {
case x ... z:
} we are expecting that
no. if you want to use an arbitrary expression, use switch without an expression. same as today. |
I have to say I've missed this in a few hot paths where using a naked switch just isn't as efficient. However, in those very few cases, manually listing the items is still an option, and what I'd go for if performance is the priority. If I remember correctly, the compiler does recognize ranges if you list them out explicitly. |
If they don't need to be constant expressions, this is a bit tricky to implement because the compiler doesn't know if |
If they are not necessarily constants, what else the compiler need to know about them except their type? here's a quote from the Go spec:
This requirement is checked on the compile time. I'm expecting that the checking for the Case-Ranges also will be in the compile time.
If your case clause declared with constants, I'm expecting from the compiler to give an informative error. If Thank you for the activity guys! these are really good points. I must say that I didn't even think about them at the first. |
This is entirely syntactic sugar. In a switch on the expression We would want to define various additional conditions for the case where We would have to explain what happens when At present switches translate into code using All in all this seems to add a fair amount of complexity to the spec, all to avoid writing Closing. |
I was hoping for some syntax such as
|
I propose adding Case-Ranges to the expression switch statement.
Case-Ranges allows to specify a range of consecutive values in a single case clause, like this:
An example:
Go's switch statement is really powerful, and I really enjoy using it. however, I think we can
improve it and make it more robust than it is.
Case-Ranges simplifies the code and makes it more readable than it is today.
Current alternatives compared to the new spec
An example of using the expression-switch statement.
We can make the code more readable, and remove the expression from the switch statment.
With Case-Ranges it will be much more readable, simplified, and fun.
Impact
No impact on existing code.
The text was updated successfully, but these errors were encountered: