Created
May 7, 2021 06:04
-
-
Save timnew/3863274c4ffcebe5118cd53eefebac0f to your computer and use it in GitHub Desktop.
A more complex example of the type issue
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
abstract class Stated<T> { | |
const Stated._(); | |
factory Stated.idle() => const _Idle<Never>(); | |
const factory Stated.value(T value) = _Value; | |
TypeChecker<T> typeChecker() => TypeChecker<T>(); | |
void someMethod(Future<T> future) async { | |
print(await future); | |
} | |
} | |
class _Idle<T> extends Stated<T>{ | |
const _Idle(): super._(); | |
} | |
class _Value<T> extends Stated<T>{ | |
final T value; | |
const _Value(this.value): super._(); | |
} | |
class TypeChecker<T> { | |
TypeChecker(); | |
bool checkType(Object another) => another.runtimeType == T; | |
} | |
void doSomething<T>(Stated<T> stated, Future<T> future){ | |
stated.someMethod(future); | |
} | |
void main(){ | |
late Stated<String> stated; | |
void runTest(){ | |
print(stated.typeChecker().checkType("Not always true")); | |
doSomething(stated, Future.value("not always works")); | |
} | |
stated = Stated.value("value"); | |
runTest(); | |
stated = Stated.idle(); | |
runTest(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment