-
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: multivalue switch #40353
Comments
You can kind of do this today:
|
Can you point to a couple of examples of existing code that would benefit from this feature? It's not obvious to me that this comes up very often. |
It has come up internally, so I cannot point to public source. More abstractly, this is useful when consuming multivalue returns where none of the elements are errors[0] and there are distinct categories for values. I think the path processing is a decent example, but a get date-time would fit too: func now() (year, month, date uint) switch now() {
case (_, 1, _):
// if january
case (2020, _, _):
// if 2020 (and not january)
} 0] Technically any non-trivial check that needs to be performed is also problematic. This includes |
Thanks. I understand that this is potentially useful. But it also introduces new concepts into the language that don't already exist, namely the parenthesized tuples in case statements. And we all agree that it doesn't provide any capabilities that can't already be done; it's just some syntactic sugar. Every change has a cost, and here the cost is new syntactic constructs that don't exist anywhere else. It leads to questions like "why can't I write So given that cost, I think we need to see a pretty strong benefit. One way to show a benefit is to show existing code that would use this. |
I am unclear how best to assess the impact to existing code, since the workarounds are pretty heterogeneous, and my anecdotes will not build a strong case for impact. One thought would be to look for the number of multireturn functions that contain 2+ non- I tried to pull structure from the current return syntax, Alternatively, it may be better to design a functional var s struct{ x, y string }
// ...
switch s {
case { x: "foo" }:
// ...
} |
Would like to see this implemented |
@ianlancetaylor: can you add this to the proposals project? |
Language change, so leaving in Go 2 process. |
Yeah, my bad. |
Per discussion above, this is a likely decline. Leaving open for four weeks for final comments. |
No further discussion. |
description
Many functions use multivalue returns, which are non-trivial to integrate into switch statements. I propose adding support to switch statements for accepting multivalue inputs:
extensions
I would think we also want to allow people to specify pseudo-tuples inline:
Since this is effectively destructuring, we should probably allow usage of the
_
symbol:pitfalls
This does not play nice with
errors.Is
, sinceswitch
only uses runtime equality checking and operator overloading is not supported, however the old syntax would work fine:costs
This is effectively syntax sugar for:
The complexity to the parser and compiler could be non-trivial, but this remains to be seen. I feel the readability of destructuring is a real win.
The text was updated successfully, but these errors were encountered: