Last active
August 2, 2023 15:50
-
-
Save ltOgt/6960b03d98a49854d2f1f30820f3dbe7 to your computer and use it in GitHub Desktop.
check if future
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
typedef _Future = Future Function(); | |
typedef _Void = void Function(); | |
typedef _FutureOrVoid = FutureOr<void> Function(); | |
main () { | |
_FutureOrVoid f; | |
f = () async {}; | |
print(f is _Future); // true | |
print(f is _Void); // TRUE | |
print(f is _FutureOrVoid); // TRUE | |
f = () {}; | |
print(f is _Future); // false | |
print(f is _Void); // TRUE | |
print(f is _FutureOrVoid); // TRUE | |
} | |
// =============== | |
import 'dart:async'; | |
typedef FutureInt = Future<int> Function(); | |
typedef Int = int Function(); | |
typedef FutureOrInt = FutureOr<int> Function(); | |
main () { | |
Base either; | |
either = InstanceFuture(); | |
print(either.isFuture); // true | |
print(either.f is FutureInt); // true | |
print(resolveOpaqueFutureInt() is FutureInt); // true | |
either = InstanceInt(); | |
print(either.isFuture); // false | |
print(either.f is FutureInt); // false | |
print(resolveOpaqueInt() is FutureInt); // false | |
} | |
// always chooses first but is opaque | |
FutureOrInt resolveOpaqueFutureInt() => DateTime.now().isAfter(DateTime.fromMicrosecondsSinceEpoch(0)) | |
? (InstanceFuture() as Base).f | |
: (InstanceInt() as Base).f; | |
// always chooses second but is opaque | |
FutureOrInt resolveOpaqueInt() => DateTime.now().isBefore(DateTime.fromMicrosecondsSinceEpoch(0)) | |
? (InstanceFuture() as Base).f | |
: (InstanceInt() as Base).f; | |
abstract class Base { | |
FutureOrInt get f; | |
bool get isFuture => f is FutureInt; | |
} | |
class InstanceFuture extends Base { | |
@override | |
FutureInt get f => () async => 1; | |
} | |
class InstanceInt extends Base { | |
@override | |
Int get f => () => 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment