dart-lang / dart-lang/language
Allow variable binding on String patterns
Nobody has claimed this yet.
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
I didn't find a duplicate for this issue, but GitHub's search is terrible, so this may be a duplicate.
I was thinking that we could allow variable bindings to String patterns. For instance:
```dart
switch (myRoute) {
'/books/${final id}/author' => 'The author of the book with ID $id',
'/books/${final id}' => 'Book with ID $id',
'/books' => 'Books list',
_ => 'Invalid route',
}
```
We could do it in every way that it's possible today to do with other patters, and if we could even extend it to support assignment.
```dart
final '/books/${bookId}' = myRoute; // We are sure that `myRoute` have the expected pattern. If not, a runtime error is thrown.
final id = int.parse(bookId);
```
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue names no files, tests, or entry points. Start by reviewing Dart's existing pattern-matching rules and the String pattern examples in the issue; done would require an agreed design for variable bindings, assignments, and runtime behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100