-
Notifications
You must be signed in to change notification settings - Fork 4k
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: Avoiding problems with confusing current switch functionality with pattern matching #8548
Comments
(1) The proposed syntax wasn't |
Which spec is that? The only one I know of is the Pattern matching for C# one, which conflicts with the current implementation on the feature/patterns branch and doesn't really make much sense. The very first definition is recursively nonsensical for example:
It's worth checking out Bill Wagner's presentation to NDC last month. If I've remembered it correctly, |
It was recursively nonsensical from version 1.0. |
If it's made no sense from v1 and still isn't fixed, I'm guessing I'm being naive in thinking we might get a spec that makes sense at some stage, then? |
I wonder how this thing even works with incredibly recursively nonsensical grammar. Sure, I'm no expert, but you should definitely contribute to this hugely messed up design. #MakeC#GreatAgain |
Again, I can only assume that Bill Wagner's presentation was an accurate representation of the current thinking of the design team. As he explicitly talked about dropping |
The problem is a "chicken and egg" one. As the spec makes no sense, I can't work out what the full syntax for pattern matching is. As the current code apparently doesn't match the current thinking on the topic with the design team, I'm stuck not knowing where to start in trying to fix the spec... :( |
@DavidArno That presentation was not made by Microsoft nor any member of the C# team. I would not consider it as authoritative. I agree that the spec seriously needs some love. |
Regarding "problem" 1: we intend to generate the same good code in both cases. Regarding "problem" 2: we do not intend to introduce this problem by adding the restriction you suggest. Whether or not |
I take it back: you were right and matter is not up for discussion! :) David
|
Oh man, I didn't mean that at all! I only meant that I think that these discussions had already bounced around a couple of times. I think revisiting conclusions is a good thing, particularly in light of new ideas. |
An early proposal around pattern matching was to use
match
to denote the start of a match expression or statement. This idea met with resistance as it risked causing a breaking change to the language, as the compiler, upon encountering:could not easily distinguish between a method called
match
or the keyword.However, using
switch
brings it's own problems.Problem 1: accidentally preventing optimisation of a
switch
Currently, a typical switch might look like:
With C# 7, this could be written as:
Whilst that small change from
default
tocase *
might seem innocent enough, it can have performance implications. Currently, the order of thecase
statements in aswitch
is unimportant, for only one case can ever be matched for a given value ofo
. As such, the compiler can optimise large switches by converting them to a hash map in IL. However, a pattern match statement must have each case examined sequentially as more than one may match and the spec requires that the first match be guaranteed to be selected. This change therefore forces the compiler to treat it as a match statement and thus not apply such optimisations.* Problem 2: the break problem*
C# enforces the use of
break
,goto
orreturn
as the last statement in a case. This exists as matching ranges can be achieved through "fall through":"Fall through" makes no sense with pattern matching though, as each
case
should be a pattern to match against. Thus the following ought to be treated as an error:As such, it's been proposed that
break
be removed as a requirement for patterns. However, with the following:The compiler must check the whole switch statement to determine if it is a pattern, before reporting missing breaks if it isn't. Or the proposal to remove
break
simply gets declined.Both of these problems could be addressed by simply leaving
switch
as is and usingmatch
instead ... except that it can't as previously stated. Therefore a solution could be to work around that breaking change. A couple of possible solutions are:Use a contextual keyword pair
If
match
is used with another keyword, in this caseon
, though that's only an example, the two together becomes a lexical sequence that cannot currently exist, and so will not cause a break change.Re-use an existing keyword to make
match
context-safeBy using a reserved word in a new way, the code can be guaranteed to not conflict with existing code (eg,
using
currently must be followed by([var|<type>] <identifier> = <expression>)
and the like and thususing <identifier> match
would be easily distinguished by the compiler in a safe fashion.There are no doubt other options that could be used too. The point being that just because
match
by itself would cause breaking-change problems, there's no need to cause other non-breaking-change problems by re-usingswitch
instead.The text was updated successfully, but these errors were encountered: