@@ -1335,9 +1335,9 @@ To type check a pattern `p` being matched against a value of type `M`:
1335
1335
1336
1336
* ** Relational** : If the operator is a comparison (` < ` , ` <= ` , ` > ` , or ` >= ` ),
1337
1337
then it is a compile-time error if ` M ` does not define that operator, if the
1338
- type of the constant in the relational pattern is not a subtype of the
1339
- operator's parameter type, or if the operator's return type is not ` bool ` .
1340
- * The ` == ` and ` != ` operators are valid for all pairs of types.*
1338
+ type of the constant in the relational pattern is not assignable to the
1339
+ operator's parameter type, or if the operator's return type is not ` bool ` or
1340
+ ` dynamic ` . * The ` == ` and ` != ` operators are valid for all pairs of types.*
1341
1341
1342
1342
* ** Null-check** or ** null-assert** :
1343
1343
@@ -1474,6 +1474,11 @@ To type check a pattern `p` being matched against a value of type `M`:
1474
1474
It is a compile-time error if the type of an expression in a guard clause is not
1475
1475
`bool` or `dynamic`.
1476
1476
1477
+ ### Switch expression type
1478
+
1479
+ The static type of a switch expression is the least upper bound of the static
1480
+ types of all of the case expressions.
1481
+
1477
1482
## Refutable and irrefutable patterns
1478
1483
1479
1484
Patterns appear inside a number of other constructs in the language. This
@@ -1665,9 +1670,10 @@ behavior.
1665
1670
to the next case (or default clause or exit the switch if there are no
1666
1671
other cases).
1667
1672
1668
- 2 . If there is a guard clause, evaluate it. If it does not evaluate to
1669
- a Boolean, throw a runtime exception. If it evaluates to ` false ` ,
1670
- continue to the next case (or default or exit).
1673
+ 2 . If there is a guard clause, evaluate it. If it does not evaluate to a
1674
+ Boolean, throw a runtime exception. * This can happen if the guard
1675
+ expression's type is ` dynamic ` .* If it evaluates to ` false ` , continue to
1676
+ the next case (or default or exit).
1671
1677
1672
1678
3 . Execute the nearest non-empty case body at or following this case.
1673
1679
* You're allowed to have multiple empty cases where all preceding
@@ -1775,25 +1781,28 @@ To match a pattern `p` against a value `v`:
1775
1781
1776
1782
* ** Variable** :
1777
1783
1778
- 1 . If ` v ` is not a subtype of ` p ` then the match fails.
1784
+ 1 . If the runtime type of ` v ` is not a subtype of the static type of ` p `
1785
+ then the match fails.
1779
1786
1780
1787
2 . Otherwise, bind the variable's identifier to ` v ` and the match succeeds.
1781
1788
1782
1789
* ** Cast** :
1783
1790
1784
- 1 . If ` v ` is not a subtype of ` p ` then throw a runtime exception. * Note
1785
- that we throw even if this appears in a refutable context. The intent
1786
- of this pattern is to assert that a value * must* have some type.*
1791
+ 1 . If the runtime type of ` v ` is not a subtype of the static type of ` p `
1792
+ then throw a runtime exception. * Note that we throw even if this appears
1793
+ in a refutable context. The intent of this pattern is to assert that a
1794
+ value * must* have some type.*
1787
1795
1788
1796
2 . Otherwise, bind the variable's identifier to ` v ` and the match succeeds.
1789
1797
1790
1798
* ** Grouping** : Match the subpattern against ` v ` and succeed if it matches.
1791
1799
1792
1800
* ** List** :
1793
1801
1794
- 1 . If ` v ` is not a subtype of ` p ` then the match fails. * The list pattern's
1795
- type will be ` List<T> ` for some ` T ` determined either by the pattern's
1796
- explicit type argument or inferred from the matched value type.*
1802
+ 1 . If the runtime type of ` v ` is not a subtype of the static type of ` p `
1803
+ then the match fails. * The list pattern's type will be ` List<T> ` for
1804
+ some ` T ` determined either by the pattern's explicit type argument or
1805
+ inferred from the matched value type.*
1797
1806
1798
1807
2 . If the length of the list determined by calling ` length ` is not equal to
1799
1808
the number of subpatterns, then the match fails. * This match failure
@@ -1811,10 +1820,10 @@ To match a pattern `p` against a value `v`:
1811
1820
1812
1821
* ** Map** :
1813
1822
1814
- 1 . If ` v ` is not a subtype of ` p ` then the match fails. * The map pattern's
1815
- type will be ` Map<K, V> ` for some ` K ` and ` V ` determined either by the
1816
- pattern's explicit type arguments or inferred from the matched value
1817
- type.*
1823
+ 1 . If the runtime type of ` v ` is not a subtype of the static type of ` p `
1824
+ then the match fails. * The map pattern's type will be ` Map<K, V> ` for
1825
+ some ` K ` and ` V ` determined either by the pattern's explicit type
1826
+ arguments or inferred from the matched value type.*
1818
1827
1819
1828
2 . Otherwise, for each entry in ` p ` :
1820
1829
@@ -1832,7 +1841,8 @@ To match a pattern `p` against a value `v`:
1832
1841
1833
1842
* ** Record** :
1834
1843
1835
- 1 . If ` v ` is not a record with the same type as ` p ` , then the match fails.
1844
+ 1 . If the runtime type of ` v ` is not a record type with the same type as
1845
+ the static type of ` p ` , then the match fails.
1836
1846
1837
1847
2 . For each field ` f ` in ` p ` , in source order:
1838
1848
@@ -1845,7 +1855,8 @@ To match a pattern `p` against a value `v`:
1845
1855
1846
1856
* ** Extractor** :
1847
1857
1848
- 1 . If ` v ` is not a subtype of ` p ` then the match fails.
1858
+ 1 . If the runtime type of ` v ` is not a subtype of the static type of ` p `
1859
+ then the match fails.
1849
1860
1850
1861
3 . Otherwise, for each field ` f ` in ` p ` :
1851
1862
@@ -1940,6 +1951,16 @@ Here is one way it could be broken down into separate pieces:
1940
1951
1941
1952
## Changelog
1942
1953
1954
+ ### 2.1
1955
+
1956
+ Minor tweaks:
1957
+
1958
+ - Define the static type of switch expressions (#2380 ).
1959
+
1960
+ - Clarify semantics of runtime type tests (#2385 ).
1961
+
1962
+ - Allow relational operators whose return type is ` dynamic ` .
1963
+
1943
1964
### 2.0
1944
1965
1945
1966
Major redesign of the syntax and minor redesign of the semantics.
0 commit comments