-
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
Two small changes to the pattern matching spec #7703
Comments
Example: int i = 1;
bool b = false;
switch (i) {
case 2 when b == true:
Console.WriteLine("case 2 when b == true");
break;
case 2:
Console.WriteLine("case 2");
b = true;
goto case (i + 1);
default:
Console.WriteLine("default");
goto case 2;
}; Would print:
|
@HaloFour That is a useful example for the spec, thanks! |
Curious, does the following loop on i, or break in one of the other locations? int i = ...;
switch (i)
{
case i - 1:
Console.WriteLine("i - 1");
break;
case i:
Console.WriteLine("i");
goto case i++;
case i + 1:
Console.WriteLine("i + 1");
break;
} Bigger question is, does it do what it really should do? Is there a preference? EDIT: changed example to include |
@bondsbw A constant-pattern doesn't accept a general variable, just "a literal, the name of a declared |
@alrz this is a constant expression, not a constant pattern. That is an existing production in the language grammar not intended to be changed by the pattern-matching spec. In any case, you are correct to say that you cannot have an expression that is not a constant. |
@gafter Yes, I meant that, well, constant-pattern defined as a constant-expression too, according to the spec. But in the "Patterns" section it is defined differently which I believe, is not intended. |
Ah, I misinterpreted "First, the expression no longer needs to be a constant." I thought that was intended for both |
Fixed in branch |
Per #206 (comment)
The use of a pattern in a
case
was intended to be an extension to the existing alternative of a constant expression. That should be clarified, though. In addition, it was intended that a case-guard could be used with a constant-valuedcase
. I will amend the spec to clarify.The
goto case
statement would be relaxed in two ways. First, the expression no longer needs to be a constant. Second, the expression no longer needs to correspond directly to acase
label. Logically thegoto case
statement would take an expression and dispatches the wholeswitch
statement with the new expression. This needs to be added to the spec too.The text was updated successfully, but these errors were encountered: