Skip to content

Record pattern matching nullability deduction #3243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
maprohu opened this issue Jul 26, 2023 · 2 comments
Open

Record pattern matching nullability deduction #3243

maprohu opened this issue Jul 26, 2023 · 2 comments
Labels
patterns Issues related to pattern matching. records Issues related to records. type-inference Type inference, issues or improvements

Comments

@maprohu
Copy link

maprohu commented Jul 26, 2023

Hi,

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

int nullFirstCompare<T extends Comparable<T>>(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

@julemand101
Copy link

I suggest you edit the issue to have a more fitting title of your problem.

@maprohu maprohu changed the title Create an issue Dart record pattern matching nullability deduction Jul 27, 2023
@maprohu maprohu changed the title Dart record pattern matching nullability deduction Record pattern matching nullability deduction Jul 27, 2023
@eernstg
Copy link
Member

eernstg commented Jul 27, 2023

We have discussed this topic before. In particular, this issue is somewhat similar: #2503, but also #2896 and #2641. In #2977 the main topic is lists, but it is still somewhat similar.

In short: That would be nice, but the discussions about how to deal with it are ongoing.

I'll transfer this issue to the language repository, it's a better fit there.

@eernstg eernstg transferred this issue from dart-lang/sdk Jul 27, 2023
@eernstg eernstg added inference patterns Issues related to pattern matching. records Issues related to records. labels Jul 27, 2023
@lrhn lrhn added type-inference Type inference, issues or improvements and removed inference labels Apr 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
patterns Issues related to pattern matching. records Issues related to records. type-inference Type inference, issues or improvements
Projects
None yet
Development

No branches or pull requests

5 participants