Skip to content

Commit 85e939d

Browse files
committed
Add a couple notes
1 parent 48a6eff commit 85e939d

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

standard/patterns.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,9 +639,14 @@ When a *pattern* appears on the right-hand-side of `is`, the extent of the patte
639639
>
640640
> <!-- Example: {template:"standalone-console", name:"LogicalPattern3", inferOutput:true} -->
641641
> ```csharp
642+
> object msg = "msg";
642643
> object obj = 5;
643644
> bool flag = true;
644645
>
646+
> // This is parsed as: (msg is (not int) or string)
647+
> result = msg is not int or string;
648+
> Console.WriteLine($"msg (\"msg\"): msg is not int or string: {result}");
649+
>
645650
> // This is parsed as: (obj is (int or string)) && flag
646651
> bool result = obj is int or string && flag;
647652
> Console.WriteLine($"obj (5), flag (true): obj is int or string && flag: {result}");
@@ -663,6 +668,7 @@ When a *pattern* appears on the right-hand-side of `is`, the extent of the patte
663668
> The output produced is
664669
>
665670
> ```console
671+
> msg ("msg"): msg is not int or string: True
666672
> obj (5), flag (true): obj is int or string && flag: True
667673
> obj (5), flag (true): obj is int || obj is string && flag: True
668674
> obj (5), flag (false): obj is int or string && flag: False
@@ -675,7 +681,9 @@ When a *pattern* appears on the right-hand-side of `is`, the extent of the patte
675681
## 11.3 Pattern subsumption
676682
677683
In a switch statement ([§13.8.3](statements.md#1383-the-switch-statement)), it is an error if a case’s pattern is *subsumed* by the preceding set of *unguarded* ([§13.8.3](statements.md#1383-the-switch-statement)) cases. In a switch expression ([§12.11](expressions.md#1211-switch-expression)), it is an error if a *switch_expression_arm*’s pattern is *subsumed* by the preceding set of *unguarded* *switch_expression_arm*s’ patterns.
684+
678685
> *Note*: This means that any input value would have been matched by one of the previous cases or arms. *end note*
686+
679687
The following rules define when a set of patterns subsumes a given pattern:
680688
681689
A pattern `P` *would match* a constant `K` if any of the following conditions hold:
@@ -721,8 +729,7 @@ A set of patterns `Q` *subsumes* a pattern `P` if any of the following condition
721729
722730
## 11.4 Pattern exhaustiveness
723731
724-
Informally, a set of patterns is exhaustive for a type if, for every possible value of that type other than null, some pattern in the set is applicable.
725-
The following rules define when a set of patterns is *exhaustive* for a type:
732+
Informally, a set of patterns is exhaustive for a type if, for every possible value of that type other than null, some pattern in the set is applicable. The following rules define when a set of patterns is *exhaustive* for a type:
726733
727734
A set of patterns `Q` is *exhaustive* for a type `T` if any of the following conditions hold:
728735
@@ -736,6 +743,9 @@ A set of patterns `Q` is *exhaustive* for a type `T` if any of the following con
736743
1. Some pattern in `Q` is a *negated_pattern* `not P₁`, and the patterns in `Q` together with the values not matched by `P₁` cover every possible value of `T`. A *negated_pattern* `not P₁` is exhaustive by itself when `P₁` matches no possible value of `T`; or
737744
1. Some pattern in `Q` is a *conjunctive_pattern* `Pand P₂`, and the set containing only `P₁` is *exhaustive* for `T` and the set containing only `P₂` is *exhaustive* for `T`.
738745
746+
> *Note*: When a type pattern includes nullable types, the pattern may be exhaustive for the type but still generate a warning because the type pattern won't match a `null` value. *end note*
747+
<!-- markdownlint-disable MD028 -->
748+
739749
> *Note*: For floating-point types, the combination of patterns `< 0` and `>= 0` is *not* exhaustive because neither relational pattern matches `NaN`. A correct exhaustive set would be `< 0`, `>= 0`, and `double.NaN` (or `float.NaN`). *end note*
740750
741751
> *Example*:
@@ -756,3 +766,4 @@ A set of patterns `Q` is *exhaustive* for a type `T` if any of the following con
756766
> ```
757767
>
758768
> *end example*
769+
<!-- markdownlint-enable MD028 -->

0 commit comments

Comments
 (0)