Skip to content

Commit 5b1115c

Browse files
committed
[patterns] Clean up and copy editing.
1 parent 3d75db9 commit 5b1115c

File tree

3 files changed

+283
-165
lines changed

3 files changed

+283
-165
lines changed

working/0546-patterns/goals-and-constraints.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ issue][546] also helps frame things.
66

77
[546]: https://github.com/dart-lang/language/issues/546
88

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
1010
goals and constraints. It's not enough to just yank pattern matching out of,
1111
say, Haskell, and cram it into Dart. We need to define what a *good* pattern
1212
matching feature looks like *in the context of Dart.*
@@ -58,7 +58,7 @@ possible strings, so this code isn't useful.
5858

5959
Patterns are also often used in places like variable declarations where users
6060
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:
6262

6363
```dart
6464
num n = ...
@@ -84,6 +84,12 @@ patterns is a useful, powerful feature, because it means that when users add new
8484
cases to some enum or enum-like type, the compiler will tell them all of the
8585
places in code that may need to be extended to handle that case.
8686

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+
8793
### Follow familiar pattern syntax and semantics
8894

8995
Pattern matching exists in some form or another in a number of languages today:
@@ -96,7 +102,7 @@ other languages.
96102

97103
### Mirror the syntax used to construct values
98104

99-
Pattern matching, especially destructuring patterns, are basically the dual to
105+
Patterns, especially destructuring patterns, are basically the dual to
100106
expressions. An expression like a list literal or constructor call takes a
101107
series of subexpressions (the list elements or constructor arguments) and
102108
bundles them together into a new composite object. A list or instance
@@ -230,9 +236,8 @@ match (value) {
230236

231237
This introduces significant complexity. We'll also want variable binding
232238
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.
236241

237242
We'll presumably want to support dotted identifiers for enum cases. I imagine
238243
users will expect that to also work for named constants imported from libraries
@@ -290,10 +295,12 @@ For example, something like this:
290295

291296
```dart
292297
abstract class Shape {}
298+
293299
class Rect extends Shape {
294300
final num left, top, right, bottom;
295301
Rect(this.left, this.top, this.right, this.bottom);
296302
}
303+
297304
class Circle extends Shape {
298305
final num x, y, radius;
299306
Circle(this.x, this.y, this.radius);

0 commit comments

Comments
 (0)