@@ -304,9 +304,10 @@ Unlike logical-or patterns, the variables defined in each branch must *not*
304304overlap, since the logical-and pattern only matches if both branches do and the
305305variables in both branches will be bound.
306306
307- If the left branch does not match, the right branch is not evaluated. * This only
308- matters because patterns may invoke user-defined methods with visible side
309- effects.*
307+ If the left branch does not match, the right branch is not evaluated. * This
308+ matters both because patterns may invoke user-defined methods with visible side
309+ effects, and because certain patterns may cause exceptions to be thrown if they
310+ are not matched (e.g. cast patterns).*
310311
311312### Relational pattern
312313
@@ -704,6 +705,9 @@ always just destructure it once with an `||` subpattern. If a user does it, it's
704705mostly like a copy/paste mistake and it's more helpful to draw their attention
705706to the error than silently accept it.*
706707
708+ It is a compile-time error if a name cannot be inferred for a named field
709+ pattern with the field name omitted (see name inference below).
710+
707711### Object pattern
708712
709713```
@@ -762,6 +766,26 @@ It is a compile-time error if:
762766 var Point(:x, x: y) = Point(1, 2);
763767 ```
764768
769+ * It is a compile-time error if a name cannot be inferred for a named getter
770+ pattern with the getter name omitted (see name inference below).
771+
772+ ### Named field/getter inference
773+
774+ In both record patterns and object patterns, the field or getter name may be
775+ elided when it can be inferred from the pattern. The inferred field or getter
776+ name for a pattern `p` is defined as follows.
777+ - If `p` is a variable pattern which binds a variable `v`, and `v` is not `_`,
778+ then the inferred name is `v`.
779+ - If `p` is `q?` then the inferred name of `p` (if any) is the inferred name
780+ of `q`.
781+ - If `p` is `q!` then the inferred name of `p` (if any) is the inferred name
782+ of `q`.
783+ - If `p` is `q as T` then the inferred name of `p` (if any) is the inferred
784+ name of `q`.
785+ - If `p` is `(q)` then the inferred name of `p` (if any) is the inferred
786+ name of `q`.
787+ - Otherwise, `p` has no inferred name.
788+
765789## Pattern uses
766790
767791Patterns are woven into the larger language in a few ways:
@@ -1502,7 +1526,7 @@ allowed and what their syntax is. The rules are:
15021526
15031527 *This program prints "no match" and not "match 2".*
15041528
1505- * A simple identifier in a matching context named `_` is treated as a wildcard
1529+ * A simple identifier in any context named `_` is treated as a wildcard
15061530 variable pattern. *A bare `_` is always treated as a wildcard regardless of
15071531 context, even though other variables in matching contexts require a marker.*
15081532
@@ -1624,9 +1648,9 @@ To orchestrate this, type inference on patterns proceeds in three phases:
16241648 object, and not necessarily to try to force a certain answer.*
16251649
162616502 . ** Calculate the static type of the matched value.** A pattern always occurs
1627- in the context of some matched value. For pattern variable declarations,
1628- this is the initializer. For switches and if-case constructs, it's the value
1629- being matched.
1651+ in the context of some matched value. For pattern variable declarations and
1652+ assignments, this is the initializer. For switches and if-case constructs,
1653+ it's the value being matched.
16301654
16311655 Using the pattern's type schema as a context type (if not in a matching
16321656 context), infer missing types on the value expression. This is the existing
@@ -1652,8 +1676,9 @@ To orchestrate this, type inference on patterns proceeds in three phases:
16521676
165316773. **Calculate the static type of the pattern.** Using that value type, recurse
16541678 through the pattern again downwards to the leaf subpatterns filling in any
1655- holes in the type schema. This process may also insert implicit coercions
1656- and casts from `dynamic` when values flow into a pattern during matching.
1679+ missing types in the pattern. This process may also insert implicit
1680+ coercions and casts from `dynamic` when values flow into a pattern during
1681+ matching.
16571682
16581683 *For example:*
16591684
@@ -1695,9 +1720,11 @@ declarations and pattern assignments. This is a type *schema* because there may
16951720be holes in the type:
16961721
16971722``` dart
1698- var (a, int b) = ... // Schema is `(? , int)`.
1723+ var (a, int b) = ... // Schema is `(_ , int)`.
16991724```
17001725
1726+ A missing type (or "hole") in the type schema is written as ` _ ` .
1727+
17011728The context type schema for a pattern ` p ` is:
17021729
17031730* ** Logical-and** : The greatest lower bound of the context type schemas of the
@@ -1715,7 +1742,7 @@ The context type schema for a pattern `p` is:
17151742 1. In an assignment context, the context type schema is the static type of
17161743 the variable that `p` resolves to.
17171744
1718- 1. Else if `p` has no type annotation, the context type schema is `? `.
1745+ 1. Else if `p` has no type annotation, the context type schema is `_ `.
17191746 *This lets us potentially infer the variable's type from the matched
17201747 value.*
17211748
@@ -1728,15 +1755,15 @@ The context type schema for a pattern `p` is:
17281755 // ^- Infers List<int>.
17291756 ```
17301757
1731- * **Cast**: The context type schema is `? `.
1758+ * **Cast**: The context type schema is `_ `.
17321759
17331760* **Parenthesized**: The context type schema of the inner subpattern.
17341761
17351762* **List**: A context type schema `List<E>` where:
17361763
17371764 1. If `p` has a type argument, then `E` is the type argument.
17381765
1739- 2. Else if `p` has no elements then `E` is `? `.
1766+ 2. Else if `p` has no elements then `E` is `_ `.
17401767
17411768 3. Else, infer the type schema from the elements:
17421769
@@ -1754,7 +1781,7 @@ The context type schema for a pattern `p` is:
17541781 *Else, `e` is a rest element without an iterable element type, so it
17551782 doesn't contribute to inference.*
17561783
1757- 3. If `es` is empty, then `E` is `? `. *This can happen if the list
1784+ 3. If `es` is empty, then `E` is `_ `. *This can happen if the list
17581785 pattern contains only a rest element which doesn't have a context
17591786 type schema that is known to be an `Iterable<T>` for some `T`,
17601787 like:*
@@ -1781,9 +1808,9 @@ The context type schema for a pattern `p` is:
17811808
17821809 1. If `p` has type arguments then `K`, and `V` are those type arguments.
17831810
1784- 2. Else if `p` has no entries, then `K` and `V` are `? `.
1811+ 2. Else if `p` has no entries, then `K` and `V` are `_ `.
17851812
1786- 3. Else `K` is `? ` and `V` is the greatest lower bound of the context type
1813+ 3. Else `K` is `_ ` and `V` is the greatest lower bound of the context type
17871814 schemas of all value subpatterns. *The rest element, if present, doesn't
17881815 contribute to the context type schema.*
17891816
@@ -1949,10 +1976,10 @@ To type check a pattern `p` being matched against a value of type `M`:
19491976 those, and `C` is `K`.
19501977
19511978 3. Else if `M` is `dynamic` then `K` and `V` are `dynamic` and `C` is
1952- `?`.
1979+ `_`.
1980+
1981+ 4. Else `K` and `V` are `Object?` and `C` is `_`.
19531982
1954- 4. Else `K` and `V` are `Object?` and `C` is `?`.
1955-
19561983 2. Type-check each key expression using `C` as the context type.
19571984
19581985 3. Type-check each value subpattern using `V` as the matched value type.
@@ -2097,7 +2124,7 @@ pattern is:
20972124
20982125* **Logical-and**, **cast**, **null-check**, **null-assert**,
20992126 **parenthesized**, **list**, **map**, **record**, or **object**: The union
2100- of the pattern variable sets of all of the subpatterns.
2127+ of the pattern variable sets of all of the immediate subpatterns.
21012128
21022129 The union of a series of pattern variable sets is the union of their
21032130 corresponding sets of variable names. Each variable in the resulting set is
0 commit comments