dart-lang / dart-lang/language

Should it be an error to match a non-List to a List pattern?

Open
#3,049 12 comments 5 reactions 0 assignees View on GitHub
question
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

The same questions applies to all other similar patterns (maps, sets, records, etc).

The following snippet compiles without issue but will never match:

```dart
if (node.argumentList case [_, final NamedExpression injector]) {
buffer.write(injector.expression);
}
```

That's because `node.argumentList` is not a `List`: it's an [ArgumentList](https://pub.dev/documentation/analyzer/latest/dart_ast_ast/ArgumentList-class.html) (which doesn't implement `List`).

What I _meant_ to write was:

```dart
if (node.argumentList.arguments case [_, final NamedExpression injector]) {
buffer.write(injector.expression);
}
```

As I wanted to match against [arguments](https://pub.dev/documentation/analyzer/latest/dart_ast_ast/ArgumentList/arguments.html), which is a `List`.

Shouldn't the compiler/analyzer be able to trivially determine that the first example cannot possibly match?

Or is it allowed because we could have a secret type that implements `ArgumentList` **and** `List`–essentially the issue being that `ArgumentList` lacks the final class modifier?

Contributor guide

Open the contributing guide

Research direction

Start with the issue's Dart pattern-matching examples and the linked ArgumentList and arguments API documentation, then review the discussion for the unresolved type-system question. Done would require a settled specification and compiler/analyzer behavior for impossible pattern matches, including the possibility of a type implementing both ArgumentList and List.

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
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.