Created
June 18, 2025 10:44
-
-
Save nrbnlulu/6e52c0553b4d443a31f7faac92390e49 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
sealed class Result<T, E> { | |
const Result(); | |
} | |
class Ok<T, E> extends Result<T, E> { | |
const Ok(this.value); | |
final T value; | |
} | |
class Err<T, E> extends Result<T, E> { | |
const Err(this.error); | |
final E error; | |
} | |
extension Foo<T> on T? { | |
Result<T, E> okOr<E>(E err) { | |
if (this != null) { | |
return Ok<T, E>(this); | |
} | |
return Err<T, E>(err); | |
} | |
} | |
void main() { | |
int? a = null; | |
Result<int, String> result = a.okOr("Value is null"); | |
print(result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
open in dartpad