Skip to content

Instantly share code, notes, and snippets.

@a-hamouda
Created October 31, 2021 19:43
Show Gist options
  • Save a-hamouda/853402111965bee9f62c6201e754f459 to your computer and use it in GitHub Desktop.
Save a-hamouda/853402111965bee9f62c6201e754f459 to your computer and use it in GitHub Desktop.
Either in dart
class Either<L, R> {
final L? _left;
final R? _right;
const Either._({L? left, R? right})
: _left = left,
_right = right;
factory Either.left(L left) {
return Either._(left: left, right: null);
}
factory Either.right(R right) {
return Either._(left: null, right: right);
}
void when({required void Function(L) left, required void Function(R) right}) {
if (_left != null) {
left(_left!);
} else {
right(_right!);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment