Skip to content

Commit b83abc0

Browse files
committed
Add a couple notes
1 parent 123649a commit b83abc0

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
@@ -599,9 +599,14 @@ When a *pattern* appears on the right-hand-side of `is`, the extent of the patte
599599
>
600600
> <!-- Example: {template:"standalone-console", name:"LogicalPattern3", inferOutput:true} -->
601601
> ```csharp
602+
> object msg = "msg";
602603
> object obj = 5;
603604
> bool flag = true;
604605
>
606+
> // This is parsed as: (msg is (not int) or string)
607+
> result = msg is not int or string;
608+
> Console.WriteLine($"msg (\"msg\"): msg is not int or string: {result}");
609+
>
605610
> // This is parsed as: (obj is (int or string)) && flag
606611
> bool result = obj is int or string && flag;
607612
> Console.WriteLine($"obj (5), flag (true): obj is int or string && flag: {result}");
@@ -623,6 +628,7 @@ When a *pattern* appears on the right-hand-side of `is`, the extent of the patte
623628
> The output produced is
624629
>
625630
> ```console
631+
> msg ("msg"): msg is not int or string: True
626632
> obj (5), flag (true): obj is int or string && flag: True
627633
> obj (5), flag (true): obj is int || obj is string && flag: True
628634
> obj (5), flag (false): obj is int or string && flag: False
@@ -634,9 +640,7 @@ When a *pattern* appears on the right-hand-side of `is`, the extent of the patte
634640
635641
## 11.3 Pattern subsumption
636642
637-
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)).
638-
Informally, this means that any input value would have been matched by one of the previous cases.
639-
The following rules define when a set of patterns subsumes a given pattern:
643+
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:
640644
641645
A pattern `P` *would match* a constant `K` if any of the following conditions hold:
642646
@@ -664,8 +668,7 @@ A set of patterns `Q` *subsumes* a pattern `P` if any of the following condition
664668
665669
## 11.4 Pattern exhaustiveness
666670
667-
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.
668-
The following rules define when a set of patterns is *exhaustive* for a type:
671+
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:
669672
670673
A set of patterns `Q` is *exhaustive* for a type `T` if any of the following conditions hold:
671674
@@ -679,6 +682,9 @@ A set of patterns `Q` is *exhaustive* for a type `T` if any of the following con
679682
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
680683
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`.
681684
685+
> *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*
686+
<!-- markdownlint-disable MD028 -->
687+
682688
> *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*
683689
684690
> *Example*:
@@ -699,3 +705,4 @@ A set of patterns `Q` is *exhaustive* for a type `T` if any of the following con
699705
> ```
700706
>
701707
> *end example*
708+
<!-- markdownlint-enable MD028 -->

0 commit comments

Comments
 (0)