-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: spec: switch case over range #64683
Comments
as far as i can tell, this is just to save you 2 lines of code, but make one very long line that's not easy to read |
@seankhliao no intention to cut the lines. If you logically speak, i have one value , say I check a day, i use switch case but if i have to have multiple values check like day, month , year, time under same object - that is still a comparison too why not use simple switch case(multiple values) rather involving conditional operators? This confines more saying my cases are only on these variables. Ofcourse this may not work where more conditional cases to be written. |
we declined multi value switch in #40353 |
Surprised to see like thoughts and example were also same date, year and month 😀. Had this thought for a different lang long ago , which had limited switch case back then. I feel this is good fit to consider given consuming multiple return values in switch case. |
This seems like a half-baked tuple with half-baked pattern matching. A fully baked tuple with fully baked pattern matching would let you say:
but we have recently turned down a number of proposals for both tuples (#32941) and pattern matching (e.g. #44022), so any new proposal should address their shortcomings explicitly, and in detail. |
Per the rationale in #64683 (comment), this is a likely decline. Leaving open for four weeks for final comments. |
@adonovan - I am currently not leaning to introduce new type tuples as I agree on some of these - #64457 @griesemer . However I like packing and unpacking seems relevant and distinct on its purpose Having that said, Here also I am trying similar kind though it reduces a line, my thought is to make more multiple values pattern matching. Even the above unpacking will fit into that scenario @urandom cal...
Another example here outside of range
Here is are some reference to other languages with different pattern matching style https://doc.rust-lang.org/rust-by-example/flow_control/match/destructuring/destructure_tuple.html |
No change in consensus, so declined. |
Go Programming Experience
Intermediate
Other Languages Experience
Java(Past), Python(Past)
Related Idea
Has this idea, or one like it, been proposed before?
I don't know
Does this affect error handling?
No
Is this about generics?
No , But can be handled along with generics
Proposal
When we iterate over a range and if we had to do multiple values check on switch case that looks similar to if else statement conditional check as switch statement doesn't accommodate that.
For below Example
I didn't write if else condition for this as we all know how to do that.
How about we have something like this in go as we iterate over all range data and a switch case for it
This can be extended to non range multiple declarations, accommodate different expressions, includes type as well and for generics.
Language Spec Changes
This changes switch statements.
Informal Change
No response
Is this change backward compatible?
Yes
Orthogonality: How does this change interact or overlap with existing features?
This brings in multiple values check against case statement
Would this change make Go easier or harder to learn, and why?
Easy
Cost Description
No Cost
Changes to Go ToolChain
golang language error check static and other.
Performance Costs
Don't know
Prototype
No response
The text was updated successfully, but these errors were encountered: