Skip to content

Instantly share code, notes, and snippets.

@nrbnlulu
Created June 18, 2025 10:44
Show Gist options
  • Save nrbnlulu/6e52c0553b4d443a31f7faac92390e49 to your computer and use it in GitHub Desktop.
Save nrbnlulu/6e52c0553b4d443a31f7faac92390e49 to your computer and use it in GitHub Desktop.
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);
}
@nrbnlulu
Copy link
Author

nrbnlulu commented Jun 18, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment