dart-lang / dart-lang/language

Allow variable binding on String patterns

Open
#4,158 13 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

feature
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.