@@ -206,8 +206,101 @@ match.
206
206
207
207
[ swift pattern ] : https://docs.swift.org/swift-book/ReferenceManual/Patterns.html
208
208
209
+ ### Grammar summary
210
+
211
+ Before walking through them in detail, here is a short summary of the kinds of
212
+ patterns and where they can appear. There are three basic entrypoints into the
213
+ pattern grammar:
214
+
215
+ * [ ` matcher ` ] [ matcher ] is the refutable patterns.
216
+ * [ ` binder ` ] [ binder ] is the irrefutable patterns.
217
+ * [ ` declarationBinder ` ] [ declarationBinder ] is a subset of the irrefutable
218
+ patterns that make sense as the outermost pattern in a variable declaration.
219
+ Subsetting allows reasonable code like:
220
+
221
+ ``` dart
222
+ var [a, b] = [1, 2]; // List.
223
+ var (a, b) = (1, 2); // Record.
224
+ var {1: a} = {1: 2}; // Map.
225
+ ```
226
+
227
+ While avoiding strange uses like:
228
+
229
+ ```dart
230
+ var String str = 'redundant'; // Variable.
231
+ var str as String = 'weird'; // Cast.
232
+ var definitely! = maybe; // Null-assert.
233
+ ```
234
+
235
+ [matcher]: #refutable-patterns-matchers
236
+ [binder]: #irrefutable-patterns-binders
237
+ [declarationBinder]: #irrefutable-patterns-binders
238
+
239
+ These entrypoints are wired into the rest of the language like so:
240
+
241
+ * A [pattern variable declaration][] contains a
242
+ [`declarationBinder`][declarationBinder].
243
+ * A [switch case][] contains a [`matcher`][matcher].
244
+ * A [`declarationMatcher`][declarationMatcher] pattern embeds a
245
+ [`declarationBinder`][declarationBinder] inside a refutable pattern. This is
246
+ a convenience to let you avoid repeating `var` or `final` several times
247
+ inside a matching pattern, as in:
248
+
249
+ ```dart
250
+ // List matcher containing three variable matchers:
251
+ case [var a, var b, var c]:
252
+ // Declaration matcher containing list binder
253
+ // containing three variable binders:
254
+ case var [a, b, c]:
255
+ ```
256
+
257
+ These two cases both do the same thing.
258
+
259
+ [pattern variable declaration]: #pattern-variable-declaration
260
+ [switch case]: #switch-statement
261
+ [declarationMatcher]: #declaration-matcher
262
+
263
+ Many kinds of patterns have both matcher (refutable) and binder (irrefutable)
264
+ forms. The table below shows examples of every specific pattern type and which
265
+ categories it appears in:
266
+
267
+ | Type | Decl Binder? | Binder? | Matcher? | Examples |
268
+ | ---- | ------------ | ------- | -------- | -------- |
269
+ | Record | Yes | [Yes][recordBinder] | [Yes][recordMatcher] | `(subpattern1, subpattern2)`<br>`(x: subpattern1, y: subpattern2)` |
270
+ | List | Yes | [Yes][listBinder] | [Yes][listMatcher] | `[subpattern1, subpattern2]` |
271
+ | Map | Yes | [Yes][mapBinder] | [Yes][mapMatcher] | `{"key": subpattern}` |
272
+ | Wildcard | No | [Yes][wildcardBinder] | [Yes][wildcardMatcher] | `_` |
273
+ | Variable | No | [Yes][variableBinder] | [Yes][variableMatcher] | `foo // Binder syntax.`<br>`var foo // Matcher syntax.`<br>`String str // Works in either.` |
274
+ | Cast | No | [Yes][castBinder] | No | `foo as String` |
275
+ | Null assert | No | [Yes][nullAssertBinder] | No | `subpattern!` |
276
+ | Literal | No | No | [Yes][literalMatcher] | `123`, `null`, `'string'` |
277
+ | Constant | No | No | [Yes][constantMatcher] | `foo`, `math.pi` |
278
+ | Null check | No | No | [Yes][nullCheckMatcher] | `subpattern?` |
279
+ | Extractor | No | No | [Yes][extractMatcher] | `SomeClass(subpattern1, x: subpattern2)` |
280
+ | Declaration | N/A | No | [Yes][declarationMatcher] | `var [foo, bar] // == [var foo, var bar]`<br>`var (a, b) // == (var a, var b)` |
281
+
282
+ [recordBinder]: #record-binder
283
+ [recordMatcher]: #record-matcher
284
+ [listBinder]: #list-binder
285
+ [listMatcher]: #list-matcher
286
+ [mapBinder]: #map-binder
287
+ [mapMatcher]: #map-matcher
288
+ [wildcardBinder]: #wildcard-binder
289
+ [wildcardMatcher]: #wildcard-matcher
290
+ [variableBinder]: #variable-binder
291
+ [variableMatcher]: #variable-matcher
292
+ [castBinder]: #cast-binder
293
+ [nullAssertBinder]: #null-assert-binder
294
+ [literalMatcher]: #literal-matcher
295
+ [constantMatcher]: #constant-matcher
296
+ [nullCheckMatcher]: #null-check-matcher
297
+ [extractMatcher]: #extractor-matcher
298
+
209
299
## Syntax
210
300
301
+ This proposal introduces a number different pattern forms and several places in
302
+ the language where they can be used.
303
+
211
304
Going top-down through the grammar, we start with the constructs where patterns
212
305
are allowed and then get to the patterns themselves.
213
306
0 commit comments