@@ -304,9 +304,10 @@ Unlike logical-or patterns, the variables defined in each branch must *not*
304
304
overlap, since the logical-and pattern only matches if both branches do and the
305
305
variables in both branches will be bound.
306
306
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).*
310
311
311
312
### Relational pattern
312
313
@@ -704,6 +705,9 @@ always just destructure it once with an `||` subpattern. If a user does it, it's
704
705
mostly like a copy/paste mistake and it's more helpful to draw their attention
705
706
to the error than silently accept it.*
706
707
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
+
707
711
### Object pattern
708
712
709
713
```
@@ -762,6 +766,26 @@ It is a compile-time error if:
762
766
var Point(:x, x: y) = Point(1, 2);
763
767
```
764
768
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
+
765
789
## Pattern uses
766
790
767
791
Patterns are woven into the larger language in a few ways:
@@ -1502,7 +1526,7 @@ allowed and what their syntax is. The rules are:
1502
1526
1503
1527
*This program prints "no match" and not "match 2".*
1504
1528
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
1506
1530
variable pattern. *A bare `_` is always treated as a wildcard regardless of
1507
1531
context, even though other variables in matching contexts require a marker.*
1508
1532
@@ -1624,9 +1648,9 @@ To orchestrate this, type inference on patterns proceeds in three phases:
1624
1648
object, and not necessarily to try to force a certain answer.*
1625
1649
1626
1650
2 . ** 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.
1630
1654
1631
1655
Using the pattern's type schema as a context type (if not in a matching
1632
1656
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:
1652
1676
1653
1677
3. **Calculate the static type of the pattern.** Using that value type, recurse
1654
1678
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.
1657
1682
1658
1683
*For example:*
1659
1684
@@ -1695,9 +1720,11 @@ declarations and pattern assignments. This is a type *schema* because there may
1695
1720
be holes in the type:
1696
1721
1697
1722
``` dart
1698
- var (a, int b) = ... // Schema is `(? , int)`.
1723
+ var (a, int b) = ... // Schema is `(_ , int)`.
1699
1724
```
1700
1725
1726
+ A missing type (or "hole") in the type schema is written as ` _ ` .
1727
+
1701
1728
The context type schema for a pattern ` p ` is:
1702
1729
1703
1730
* ** 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:
1715
1742
1. In an assignment context, the context type schema is the static type of
1716
1743
the variable that `p` resolves to.
1717
1744
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 `_ `.
1719
1746
*This lets us potentially infer the variable's type from the matched
1720
1747
value.*
1721
1748
@@ -1728,15 +1755,15 @@ The context type schema for a pattern `p` is:
1728
1755
// ^- Infers List<int>.
1729
1756
```
1730
1757
1731
- * **Cast**: The context type schema is `? `.
1758
+ * **Cast**: The context type schema is `_ `.
1732
1759
1733
1760
* **Parenthesized**: The context type schema of the inner subpattern.
1734
1761
1735
1762
* **List**: A context type schema `List<E>` where:
1736
1763
1737
1764
1. If `p` has a type argument, then `E` is the type argument.
1738
1765
1739
- 2. Else if `p` has no elements then `E` is `? `.
1766
+ 2. Else if `p` has no elements then `E` is `_ `.
1740
1767
1741
1768
3. Else, infer the type schema from the elements:
1742
1769
@@ -1754,7 +1781,7 @@ The context type schema for a pattern `p` is:
1754
1781
*Else, `e` is a rest element without an iterable element type, so it
1755
1782
doesn't contribute to inference.*
1756
1783
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
1758
1785
pattern contains only a rest element which doesn't have a context
1759
1786
type schema that is known to be an `Iterable<T>` for some `T`,
1760
1787
like:*
@@ -1781,9 +1808,9 @@ The context type schema for a pattern `p` is:
1781
1808
1782
1809
1. If `p` has type arguments then `K`, and `V` are those type arguments.
1783
1810
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 `_ `.
1785
1812
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
1787
1814
schemas of all value subpatterns. *The rest element, if present, doesn't
1788
1815
contribute to the context type schema.*
1789
1816
@@ -1949,10 +1976,10 @@ To type check a pattern `p` being matched against a value of type `M`:
1949
1976
those, and `C` is `K`.
1950
1977
1951
1978
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 `_`.
1953
1982
1954
- 4. Else `K` and `V` are `Object?` and `C` is `?`.
1955
-
1956
1983
2. Type-check each key expression using `C` as the context type.
1957
1984
1958
1985
3. Type-check each value subpattern using `V` as the matched value type.
@@ -2097,7 +2124,7 @@ pattern is:
2097
2124
2098
2125
* **Logical-and**, **cast**, **null-check**, **null-assert**,
2099
2126
**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.
2101
2128
2102
2129
The union of a series of pattern variable sets is the union of their
2103
2130
corresponding sets of variable names. Each variable in the resulting set is
0 commit comments