dart-lang / dart-lang/language

Record pattern matching nullability deduction

Open
#3,243 2 comments 0 reactions 0 assignees View on GitHub
patterns records type-inference
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

Hi,

I would like to implement a method in Dart, something like the following:

```dart
int nullFirstCompare>(T? a, T? b) {
return switch ((a, b)) {
(null, null) => 0,
(null, _) => -1,
(_, null) => 1,
(final a, final b) => a.compareTo(b), // does not compile
};
}
```

So, my hope would be that by the 4th case in the switch the Dart compiler would deduce that none of the record fields can be null. It does not, therefore the 4h case does not compile.

I wonder if this is a limitation or a bug in the compiler. Also if there is some elegant way to rewrite this code to make it work with the current Dart compiler.

```
% dart --version
Dart SDK version: 3.0.6 (stable) (Tue Jul 11 18:49:07 2023 +0000) on "macos_arm64"
```

Thanks!

Marton

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.