Discussion: goto case statements and pattern-matching #3610
Replies: 4 comments
-
Another idea would be to accept arbitrary expression in
I think this would be compatible with old |
Beta Was this translation helpful? Give feedback.
-
s/subsum/subsume (×6) It seems like there should be an equivalence between fallthrough and top-level |
Beta Was this translation helpful? Give feedback.
-
I think that was originally proposed by @gafter in C# 7.0 timeframe but cutted out. I believe accepting a pattern could make this more "predictable", rather than an arbitrary expression. The motivation here is to jump to a specific case label, not reevaluate the switch against a brand new expression. Besides, it would be hard to specify some case labels like |
Beta Was this translation helpful? Give feedback.
-
@jnm2 Fixed. Thanks! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently, pattern matching made
goto case
statements useless, but there exist some valid use cases, and even more with the recent additions.A simple example is when you use
case var p:
instead ofdefault:
and you no longer can usegoto default;
.Relational patterns are interesting in that it's likely that all the consecutive patterns could overlap with each other.
As for the most common use cases, we could use
fallthrough
to transfer the control to the very next section.If the subsequent pattern would not subsume the current one, pattern variables (if any) are not definitely assigned -- not so if otherwise.
Note that if the next pattern has a
when
clause, it will NOT be evaluated.If we do want to evaluate
when
clauses, we will need to use another keyword:resume
which will also evaluate case labels.To jump to an arbitrary case label we could use the old
goto case
generalized to accept a pattern:goto case <pattern>
There must exist a case label that subsumes the
<pattern>
ingoto case
, otherwise a compile-time error is produced. Variable def-assignment rules still applies depending on the current pattern, but no variables are permitted ingoto case
itself.With the above,
fallthrough;
would be a shorthand forgoto case <very next pattern>
with the exception thatwhen
clauses are permitted but not evaluated. This rule is driven from the currentgoto case
which is not permitted in presence of awhen
clause.We could also consider
next;
as a shorthand forgoto case <first subsuming pattern>
which will resolve togoto case _;
most of the time, unless there exists a more specific pattern that could be picked. In this situation, it's likely that we could useresume
instead, except that nowhen
clause is evaluated and the target is always known at the compile-time.Beta Was this translation helpful? Give feedback.
All reactions