Created
August 15, 2020 16:13
-
-
Save danielgomezrico/ac4194d20ed8824d09fd421179d23bca to your computer and use it in GitHub Desktop.
Generics failure example with dart
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 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