Skip to content

Instantly share code, notes, and snippets.

@timnew
Created May 7, 2021 06:04
Show Gist options
  • Save timnew/3863274c4ffcebe5118cd53eefebac0f to your computer and use it in GitHub Desktop.
Save timnew/3863274c4ffcebe5118cd53eefebac0f to your computer and use it in GitHub Desktop.
A more complex example of the type issue
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