xsahil03x / super_enum

Create super-powered dart enums similar to sealed classes in Kotlin
https://pub.dev/packages/super_enum
MIT License
116 stars 13 forks source link

whenXXX methods return FutureOr #27

Closed WanderingFire2001 closed 4 years ago

WanderingFire2001 commented 4 years ago

The update to 0.3 modified when() and its new sibling methods to return FutureOr.

The Effective Dart advice is to use FutureOr as a parameter, but not to return it. They advise instead to just return Future, as it will be more clear to users (who need to await the result either way).

https://dart.dev/guides/language/effective-dart/design#avoid-using-futureort-as-a-return-type

So the generator would produce

  Future<R> when<R>(
      {@required FutureOr<R> Function(Success) success,
      @required FutureOr<R> Function(Error) error}) async {
}

with Future / async instead of FutureOr

  FutureOr<R> when<R>(
      {@required FutureOr<R> Function(Success) success,
      @required FutureOr<R> Function(Error) error}) {
}

(That may also save you from having to document how to use the return types, as many of your users won't have experience dealing with a FutureOr return.)

Thanks for the new whenXXX methods, and for fixing the generation of null props for @objects!

passsy commented 4 years ago

I agree with effective dart that it is confusing for users to receive FutureOr because they are required to cast before they know whether it is a Future or not.

My initial proposal only recommended FutureOr in case void is returned. This would be fine because users aren't interested in the value and never have to cast.

It seems like it was also changed for all the other methods as well. I don't think this is necessary. Return type R was/is fine.

xsahil03x commented 4 years ago

@passsy @astralstriker we should make a decision on this and close this ASAP as many users are getting confused with FutureOr.