Skip to content
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

Closed
gafter opened this issue Dec 26, 2015 · 8 comments
Closed

Two small changes to the pattern matching spec #7703

gafter opened this issue Dec 26, 2015 · 8 comments
Assignees
Labels
Area-Language Design New Language Feature - Pattern Matching Pattern Matching Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented Verification Not Required

Comments

@gafter
Copy link
Member

gafter commented Dec 26, 2015

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-valued case. 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 a case label. Logically the goto case statement would take an expression and dispatches the whole switch statement with the new expression. This needs to be added to the spec too.

@gafter gafter self-assigned this Dec 26, 2015
@gafter gafter added this to the 1.2 milestone Dec 26, 2015
@HaloFour
Copy link

@gafter

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:

default
case 2
case 2 when b == true

@gafter
Copy link
Member Author

gafter commented Dec 29, 2015

@HaloFour That is a useful example for the spec, thanks!

@bondsbw
Copy link

bondsbw commented Dec 30, 2015

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 i - 1 case

@alrz
Copy link
Member

alrz commented Dec 30, 2015

@bondsbw A constant-pattern doesn't accept a general variable, just "a literal, the name of a declared const variable, or an enumeration constant". However, earlier in the spec, it is defined as a shift-expression which is not correct I guess.

@gafter
Copy link
Member Author

gafter commented Dec 30, 2015

@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.

@alrz
Copy link
Member

alrz commented Dec 30, 2015

@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.

@bondsbw
Copy link

bondsbw commented Dec 30, 2015

Ah, I misinterpreted "First, the expression no longer needs to be a constant." I thought that was intended for both goto case and case.

@gafter
Copy link
Member Author

gafter commented Mar 21, 2016

Fixed in branch features/patterns.

@gafter gafter closed this as completed Mar 21, 2016
@gafter gafter added Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented Verification Not Required and removed 3 - Working labels Mar 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Language Design New Language Feature - Pattern Matching Pattern Matching Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented Verification Not Required
Projects
None yet
Development

No branches or pull requests

5 participants