-
Notifications
You must be signed in to change notification settings - Fork 88
Add support for pattern additions #1026
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
base: draft-v9
Are you sure you want to change the base?
Add support for pattern additions #1026
Conversation
Regarding the OP, I have the following comments: (1) Re "Type patterns: Grammar rule primary_pattern is defined as something different from pattern, but as best as I can tell, they are exactly the same thing, so I replaced all occurrences of primary_pattern with pattern. Is that OK? (Also, the use of primary suggests here are secondary/other forms, but I don't see what they are.)" Several kinds of (2) Re: "Relational patterns: the proposed grammar shows relational_expression as the operand of each relational pattern, but the accompanying narrative goes on to constrain such relational expressions to constants only. So what not use *constant_expression" instead? And that's what I did. Am I missing something?" Yes, there is a reason. The grammar rule for
Because the left operand of the (3) Re: "Relational patterns: The proposal did not include support for enums, but as they are supported, I included them in the spec." Yes, good. (4) Re: "Logical patterns: The proposal calls these pattern combinators; however, that term is not used elsewhere on MS's tutorial pages, so I used the more obvious (and elsewhere used) logical patterns instead." Yes, good. (5) Re: "Logical patterns: The fact that the rule logical_pattern always expands to disjunctive_pattern may seem unnecessary, but it allows the placement of logical_pattern in the rule pattern rather than pushing the (odd-looking) disjunctive_pattern up there instead. (We use this approach elsewhere in the grammar.)" Okay, but I don't see a problem with using (6) Re: "The (substantial) second half of the proposal, "Open Issues with Proposed Changes," raises questions/issues. However, unlike all (or at least most) other MS proposals I've worked with, there is no follow-on discussion to say how these topics were actually addressed in the final implementation. Certainly, if any of the suggested approaches were taken, more words will need to be added to this PR to reflect that. I'll need help from someone at MS to resolve this." We should open issues for each of the open issues (or one issue for all of them) to make sure we address them. |
@@ -2,7 +2,7 @@ | |||
|
|||
## 11.1 General | |||
|
|||
A ***pattern*** is a syntactic form that can be used with the `is` operator ([§12.12.12](expressions.md#121212-the-is-operator)) and in a *switch_statement* ([§13.8.3](statements.md#1383-the-switch-statement)) to express the shape of data against which incoming data is to be compared. A pattern is tested against the *expression* of a switch statement, or against a *relational_expression* that is on the left-hand side of an `is` operator, each of which is referred to as a ***pattern input value***. | |||
A ***pattern*** is a syntactic form that can be used with the `is` operator ([§12.12.12](expressions.md#121212-the-is-operator)) and in a *switch_statement* ([§13.8.3](statements.md#1383-the-switch-statement)) to express the shape of data against which incoming data is to be compared. A pattern is tested against the *expression* of a switch statement, or against a *relational_expression* that is on the left-hand side of an `is` operator, each of which is referred to as a ***pattern input value***. Patterns may be combined using Boolean logic. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know what "Patterns may be combined using Boolean logic" means. Yes, there are pattern forms for disjunction, conjunction, and negation, but the "Boolean logic" sentence suggests something more general.
|
||
Relational patterns support the relational operators `<`, `<=`, `>`, and `>=` on all of the built-in types that support such binary relational operators with both operands having the same type: `sbyte`, `byte`, `short`, `ushort`, `int`, `uint`, `long`, `ulong`, `char`, `float`, `double`, `decimal`, `nint`, `nuint`, and enums. | ||
|
||
It is a compile-time error if `constant_expression`is `double.NaN`, `float.NaN`, or `null_literal`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not exactly. It is not a syntactic constraint. It is a compile-time error if it evaluates to one of those values.
|
||
§logical-pattern-new-clause Logical pattern | ||
|
||
A *logical_pattern* is used to negate a pattern input value ([§11.1](patterns.md#111-general)) or to combine that value with a pattern using a Boolean operator. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it doesn't negate the input value. It inverts (in the boolean sense) the result of the pattern-match. Similarly, the combination doesn't happen with the value, but with the result of pattern-match tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If is an operation of many names: not, invert & negate, and in C# it is indeed called logical negation (§12.9.4).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not concerned with which of those names we use. I'm pointing out that the meaning is not "to negate a pattern input value", but rather to negate the result of determining whether or not the input value matches the pattern.
|
||
> *Note*: As indicated by the grammar, `not` has precedence over `and`, which has precedence over `or`. This can be explicitly indicated or overridden by using parentheses. *end note* | ||
|
||
When a *pattern* is used with `is`, any pattern operators in that *pattern* have higher precedence than their logical operator counterparts. Otherwise, those pattern operators have lower precedence. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what this means. An example would be helpful.
@@ -12,9 +12,13 @@ A pattern may have one of the following forms: | |||
|
|||
```ANTLR | |||
pattern | |||
: declaration_pattern | |||
: '(' pattern ')' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is worth a sentence to say what this means.
This PR might be incomplete; read below.
Regarding the MS proposal:
This PR was created based on the assumption that the edits proposed by PR #873 for V8-related pattern-matching features will be adopted, and no other non-trivial edits will be made to that part of V8. When that PR is finally merged, this PR might need adjustment to reflect any changes in the V8 proposal review/adoption.