@@ -2155,10 +2155,30 @@ alt_pat : pat [ "to" pat ] ? [ "if" expr ] ;
2155
2155
An ` alt ` expression branches on a * pattern* . The exact form of matching that
2156
2156
occurs depends on the pattern. Patterns consist of some combination of
2157
2157
literals, destructured enum constructors, records and tuples, variable binding
2158
- specifications and placeholders (` _ ` ). An ` alt ` expression has a * head
2158
+ specifications, wildcards ( ` * ` ), and placeholders (` _ ` ). An ` alt ` expression has a * head
2159
2159
expression* , which is the value to compare to the patterns. The type of the
2160
2160
patterns must equal the type of the head expression.
2161
2161
2162
+ In a pattern whose head expression has an ` enum ` type, a placeholder (` _ ` ) stands for a
2163
+ * single* data field, whereas a wildcard ` * ` stands for * all* the fields of a particular
2164
+ variant. For example:
2165
+
2166
+ ~~~~
2167
+ enum list<X> { nil, cons(X, @list<X>) }
2168
+
2169
+ let x: list<int> = cons(10, @cons(11, @nil));
2170
+
2171
+ alt x {
2172
+ cons(_, @nil) { fail "singleton list"; }
2173
+ cons(*) { ret; }
2174
+ nil { fail "empty list"; }
2175
+ }
2176
+ ~~~~
2177
+
2178
+ The first pattern matches lists constructed by applying ` cons ` to any head value, and a
2179
+ tail value of ` @nil ` . The second pattern matches ` any ` list constructed with ` cons ` ,
2180
+ ignoring the values of its arguments.
2181
+
2162
2182
To execute an ` alt ` expression, first the head expression is evaluated, then
2163
2183
its value is sequentially compared to the patterns in the arms until a match
2164
2184
is found. The first arm with a matching pattern is chosen as the branch target
0 commit comments