Skip to content
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

Replace when and maybeWhen with pattern matching in Dart 3 #333

Open
satvikpendem opened this issue Dec 16, 2023 · 5 comments
Open

Replace when and maybeWhen with pattern matching in Dart 3 #333

satvikpendem opened this issue Dec 16, 2023 · 5 comments
Labels
enhancement New feature or request planned

Comments

@satvikpendem
Copy link

Now that Dart 3 is here, you can likely replace the above two functions without loss of functionality, as freezed has also done here:

@freezed
sealed class Example with _$Example {
  const factory Example.person(String name, int age) = Person;
  const factory Example.city(String name, int population) = City;
}

...

switch (example) {
  Person(:final name) => print('Person $name'),
  City(:final population) => print('City ($population)'),
}
Copy link

👋 @satvikpendem
Thank you for raising an issue. I will investigate the issue and get back to you as soon as possible.
Please make sure you have provided enough context.

This library is created and maintained by me, @budde377. Please consider supporting my work and ensure our survival by donating here.

@budde377
Copy link
Contributor

Thanks for raising this suggestion! I've been meaning to take a look at what faff we can remove and this seems like a good option.

@budde377 budde377 added enhancement New feature or request planned labels Mar 22, 2024
@coladarci
Copy link

I'm curious - if this is tackled, will it remove the need to also specify an orElse equivalent?

If a given object can only be one of 2 object-types, it feels odd that we always have to provide an orElse. To get around this we do this in many, many places :)

    thing.when(
      objectTypeOne: (one) => ...,
      objectTypeTwo: (two) => ...,
      orElse: () => throw Exception('Can not happen.'),
    );

Which works fine but is a little silly.

@budde377
Copy link
Contributor

budde377 commented Jun 7, 2024 via email

@coladarci
Copy link

depending on your set up, you might not be able to ensure that all your clients are updated.

Thank you for this - a very good reason to have this, indeed...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request planned
Projects
None yet
Development

No branches or pull requests

3 participants