@@ -6,7 +6,7 @@ issue][546] also helps frame things.
6
6
7
7
[ 546 ] : https://github.com/dart-lang/language/issues/546
8
8
9
- Before I get into details of the design, I wanted to walk through our specific
9
+ Before I get into details of the design, I want to walk through our specific
10
10
goals and constraints. It's not enough to just yank pattern matching out of,
11
11
say, Haskell, and cram it into Dart. We need to define what a * good* pattern
12
12
matching feature looks like * in the context of Dart.*
@@ -58,7 +58,7 @@ possible strings, so this code isn't useful.
58
58
59
59
Patterns are also often used in places like variable declarations where users
60
60
don't expect a runtime failure to occur. In that case, if a pattern * might* fail
61
- to match, it should probably be compile-time error:
61
+ to match, it should probably be a compile-time error:
62
62
63
63
``` dart
64
64
num n = ...
@@ -84,6 +84,12 @@ patterns is a useful, powerful feature, because it means that when users add new
84
84
cases to some enum or enum-like type, the compiler will tell them all of the
85
85
places in code that may need to be extended to handle that case.
86
86
87
+ In object-oriented languages, you get a compile error if a subclass fails to
88
+ implement some abstract method. This ensures that all calls to that polymorphic
89
+ method definitely reach a defined method. In functional languages exhaustiveness
90
+ checks, on pattern matches is the corresponding way to ensure an operation
91
+ supports all types it can be applied to.
92
+
87
93
### Follow familiar pattern syntax and semantics
88
94
89
95
Pattern matching exists in some form or another in a number of languages today:
@@ -96,7 +102,7 @@ other languages.
96
102
97
103
### Mirror the syntax used to construct values
98
104
99
- Pattern matching , especially destructuring patterns, are basically the dual to
105
+ Patterns , especially destructuring patterns, are basically the dual to
100
106
expressions. An expression like a list literal or constructor call takes a
101
107
series of subexpressions (the list elements or constructor arguments) and
102
108
bundles them together into a new composite object. A list or instance
@@ -230,9 +236,8 @@ match (value) {
230
236
231
237
This introduces significant complexity. We'll also want variable binding
232
238
patterns, which are also naturally represented as an identifier. If we allow
233
- constant patterns to also be simple bare identifiers, it means we need to do
234
- lexical scope resolution to determine if a pattern is a constant pattern or a
235
- variable pattern.
239
+ constant patterns to also be simple bare identifiers, it means we need way to
240
+ distinguish constant patterns from variable patterns.
236
241
237
242
We'll presumably want to support dotted identifiers for enum cases. I imagine
238
243
users will expect that to also work for named constants imported from libraries
@@ -290,10 +295,12 @@ For example, something like this:
290
295
291
296
``` dart
292
297
abstract class Shape {}
298
+
293
299
class Rect extends Shape {
294
300
final num left, top, right, bottom;
295
301
Rect(this.left, this.top, this.right, this.bottom);
296
302
}
303
+
297
304
class Circle extends Shape {
298
305
final num x, y, radius;
299
306
Circle(this.x, this.y, this.radius);
0 commit comments