Skip to content

Commit 2eb5cba

Browse files
committed
Address lined comments
Address the comments on specific lines in #1026.
1 parent 9556bb1 commit 2eb5cba

1 file changed

Lines changed: 32 additions & 3 deletions

File tree

standard/patterns.md

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ A pattern is tested against a value in a number of contexts:
1111
- In a switch expression, the *pattern* of a *switch_expression_arm* is tested against the expression on the switch-expression’s left-hand-side.
1212
- In nested contexts, the *sub-pattern* is tested against values retrieved from properties, fields, or indexed from other input values, depending on the pattern form.
1313

14-
The value against which a pattern is tested is called the ***pattern input value***. Patterns may be combined using Boolean logic.
14+
The value against which a pattern is tested is called the ***pattern input value***.
1515

1616
## 11.2 Pattern forms
1717

@@ -34,6 +34,8 @@ pattern
3434
;
3535
```
3636

37+
The `'(' pattern ')'` production allows a pattern to be enclosed in parentheses to enforce the order of evaluation among patterns combined using one of the *logical_pattern*s.
38+
3739
Some *pattern*s can result in the declaration of a local variable.
3840

3941
Each pattern form defines the set of types for input values that the pattern may be applied to. A pattern `P` is *applicable to* a type `T` if `T` is among the types whose values the pattern may match. It is a compile-time error if a pattern `P` appears in a program to match a pattern input value ([§11.1](patterns.md#111-general)) of type `T` if `P` is not applicable to `T`.
@@ -438,7 +440,7 @@ relational_pattern
438440

439441
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.
440442

441-
It is a compile-time error if `constant_expression`is `double.NaN`, `float.NaN`, or `null_literal`.
443+
It is a compile-time error if `constant_expression` evaluates to one of `double.NaN`, `float.NaN`, or `null_literal`.
442444

443445
When the input value has a type for which a suitable built-in binary relational operator is defined, the evaluation of that operator is taken as the meaning of the relational pattern. Otherwise, the input value is converted to the type of `constant_expression` using an explicit nullable or unboxing conversion. It is a compile-time error if no such conversion exists. The pattern is considered to not match if the conversion fails. If the conversion succeeds, the result of the pattern-matching operation is the result of evaluating the expression `e «op» v` where `e` is the converted input, «op» is the relational operator, and `v` is the `constant_expression`.
444446

@@ -471,7 +473,7 @@ When the input value has a type for which a suitable built-in binary relational
471473
472474
### §logical-pattern-new-clause Logical pattern
473475
474-
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.
476+
A *logical_pattern* is used to negate the result of matching a pattern input value ([§11.1](patterns.md#111-general)) or to combine that match result with a pattern using a Boolean operator.
475477
476478
```ANTLR
477479
logical_pattern
@@ -561,6 +563,33 @@ When a *pattern* is used with `is`, any pattern operators in that *pattern* have
561563
> ```
562564
>
563565
> *end example*
566+
<!-- markdownlint-disable MD028 -->
567+
568+
<!-- markdownlint-enable MD028 -->
569+
> *Example*:
570+
>
571+
> <!-- Example: {template:"standalone-console", name:"LogicalPattern3"} -->
572+
> ```csharp
573+
> object obj = 5;
574+
> bool flag = true;
575+
>
576+
> // This is parsed as: (obj is (int or string)) && flag
577+
> bool result = obj is int or string && flag;
578+
> Console.WriteLine($"obj (5), flag (true): obj is int or string && flag: {result}"); // True
579+
>
580+
> // This is parsed as: (obj is int) || ((obj is string) && flag)
581+
> result = obj is int || obj is string && flag;
582+
> Console.WriteLine($"obj (5), flag (true): obj is int || obj is string && flag: {result}"); // True
583+
>
584+
> flag = false;
585+
> // This is parsed as: (obj is (int or string)) && flag
586+
> result = obj is int or string && flag;
587+
> Console.WriteLine($"obj (5), flag (false): obj is int or string && flag: {result}"); // False
588+
>
589+
> // This is parsed as: (obj is int) || ((obj is string) && flag)
590+
> result = obj is int || obj is string && flag;
591+
> Console.WriteLine($"obj (5), flag (false): obj is int || obj is string && flag: {result}"); // True
592+
> ```
564593
565594
## 11.3 Pattern subsumption
566595

0 commit comments

Comments
 (0)