Skip to content

Commit e6d573a

Browse files
committed
Address lined comments
Address the comments on specific lines in #1026.
1 parent 9c7988e commit e6d573a

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`.
@@ -437,7 +439,7 @@ relational_pattern
437439

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

440-
It is a compile-time error if `constant_expression`is `double.NaN`, `float.NaN`, or `null_literal`.
442+
It is a compile-time error if `constant_expression` evaluates to one of `double.NaN`, `float.NaN`, or `null_literal`.
441443

442444
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`.
443445

@@ -470,7 +472,7 @@ When the input value has a type for which a suitable built-in binary relational
470472
471473
### §logical-pattern-new-clause Logical pattern
472474
473-
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.
475+
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.
474476
475477
```ANTLR
476478
logical_pattern
@@ -560,6 +562,33 @@ When a *pattern* is used with `is`, any pattern operators in that *pattern* have
560562
> ```
561563
>
562564
> *end example*
565+
<!-- markdownlint-disable MD028 -->
566+
567+
<!-- markdownlint-enable MD028 -->
568+
> *Example*:
569+
>
570+
> <!-- Example: {template:"standalone-console", name:"LogicalPattern3"} -->
571+
> ```csharp
572+
> object obj = 5;
573+
> bool flag = true;
574+
>
575+
> // This is parsed as: (obj is (int or string)) && flag
576+
> bool result = obj is int or string && flag;
577+
> Console.WriteLine($"obj (5), flag (true): obj is int or string && flag: {result}"); // True
578+
>
579+
> // This is parsed as: (obj is int) || ((obj is string) && flag)
580+
> result = obj is int || obj is string && flag;
581+
> Console.WriteLine($"obj (5), flag (true): obj is int || obj is string && flag: {result}"); // True
582+
>
583+
> flag = false;
584+
> // This is parsed as: (obj is (int or string)) && flag
585+
> result = obj is int or string && flag;
586+
> Console.WriteLine($"obj (5), flag (false): obj is int or string && flag: {result}"); // False
587+
>
588+
> // This is parsed as: (obj is int) || ((obj is string) && flag)
589+
> result = obj is int || obj is string && flag;
590+
> Console.WriteLine($"obj (5), flag (false): obj is int || obj is string && flag: {result}"); // True
591+
> ```
563592
564593
## 11.3 Pattern subsumption
565594

0 commit comments

Comments
 (0)