heftapp / graphql_codegen

MIT License
136 stars 52 forks source link

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

Open satvikpendem opened 6 months ago

satvikpendem commented 6 months ago

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)'),
}
github-actions[bot] commented 6 months ago

đź‘‹ @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 commented 6 months ago

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.

coladarci commented 3 weeks ago

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 commented 3 weeks ago

Providing the orElse even when all other cases have been “checked” is useful if you want to build future-proof apps. It will allow you to add a new type to your API without breaking existing apps.

Say that you add a type called ObjectType3 in your example, this would throw an exception. You can then add a new case, but depending on your set up, you might not be able to ensure that all your clients are updated.

On Thu, 6 Jun 2024 at 17:50, Greg Coladarci @.***> wrote:

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.

— Reply to this email directly, view it on GitHub https://github.com/heftapp/graphql_codegen/issues/333#issuecomment-2152979124, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA2UFS5AOCH4P7DWNLNU7O3ZGCHNDAVCNFSM6AAAAABI5DQFA6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNJSHE3TSMJSGQ . You are receiving this because you were mentioned.Message ID: @.***>

coladarci commented 2 weeks ago

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...