Skip to content

Commit 94ee79d

Browse files
committed
Add a couple notes
1 parent 63eff42 commit 94ee79d

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

standard/patterns.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -598,9 +598,14 @@ When a *pattern* appears on the right-hand-side of `is`, the extent of the patte
598598
>
599599
> <!-- Example: {template:"standalone-console", name:"LogicalPattern3", inferOutput:true} -->
600600
> ```csharp
601+
> object msg = "msg";
601602
> object obj = 5;
602603
> bool flag = true;
603604
>
605+
> // This is parsed as: (msg is (not int) or string)
606+
> result = msg is not int or string;
607+
> Console.WriteLine($"msg (\"msg\"): msg is not int or string: {result}");
608+
>
604609
> // This is parsed as: (obj is (int or string)) && flag
605610
> bool result = obj is int or string && flag;
606611
> Console.WriteLine($"obj (5), flag (true): obj is int or string && flag: {result}");
@@ -622,6 +627,7 @@ When a *pattern* appears on the right-hand-side of `is`, the extent of the patte
622627
> The output produced is
623628
>
624629
> ```console
630+
> msg ("msg"): msg is not int or string: True
625631
> obj (5), flag (true): obj is int or string && flag: True
626632
> obj (5), flag (true): obj is int || obj is string && flag: True
627633
> obj (5), flag (false): obj is int or string && flag: False
@@ -633,9 +639,7 @@ When a *pattern* appears on the right-hand-side of `is`, the extent of the patte
633639
634640
## 11.3 Pattern subsumption
635641
636-
In a switch statement, it is an error if a case’s pattern is *subsumed* by the preceding set of unguarded cases ([§13.8.3](statements.md#1383-the-switch-statement)).
637-
Informally, this means that any input value would have been matched by one of the previous cases.
638-
The following rules define when a set of patterns subsumes a given pattern:
642+
In a switch statement, it is an error if a case’s pattern is *subsumed* by the preceding set of unguarded cases ([§13.8.3](statements.md#1383-the-switch-statement)). Informally, this means that any input value would have been matched by one of the previous cases. The following rules define when a set of patterns subsumes a given pattern:
639643
640644
A pattern `P` *would match* a constant `K` if any of the following conditions hold:
641645
@@ -663,8 +667,7 @@ A set of patterns `Q` *subsumes* a pattern `P` if any of the following condition
663667
664668
## 11.4 Pattern exhaustiveness
665669
666-
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.
667-
The following rules define when a set of patterns is *exhaustive* for a type:
670+
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:
668671
669672
A set of patterns `Q` is *exhaustive* for a type `T` if any of the following conditions hold:
670673
@@ -678,6 +681,9 @@ A set of patterns `Q` is *exhaustive* for a type `T` if any of the following con
678681
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
679682
1. Some pattern in `Q` is a *conjunctive_pattern* `P₁ and P₂`, and the set containing only `P₁` is *exhaustive* for `T` and the set containing only `P₂` is *exhaustive* for `T`.
680683
684+
> *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*
685+
<!-- markdownlint-disable MD028 -->
686+
681687
> *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*
682688
683689
> *Example*:
@@ -698,3 +704,4 @@ A set of patterns `Q` is *exhaustive* for a type `T` if any of the following con
698704
> ```
699705
>
700706
> *end example*
707+
<!-- markdownlint-enable MD028 -->

0 commit comments

Comments
 (0)