Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generics failure example with dart
abstract class Result<T> { }
class Success<T> extends Result<T> {
final T body;
Success(this.body);
}
class Failure<T> extends Result<T> {
final String cause;
Failure(this.cause);
}
void works(Result<String> result) {
if(result is Success<String>) {
print("Success: ${result.body}");
} else if (result is Failure<String>) {
print("Failure: ${result.cause}");
}
}
void notWorking(Result<String> result) {
if(result is Success) {
print("Success: ${result.body}");
} else if (result is Failure) {
print("Failure: ${result.cause}");
}
}
void main() {
works(Success("Something"));
works(Failure("Something"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment