Created
October 31, 2021 19:43
-
-
Save a-hamouda/853402111965bee9f62c6201e754f459 to your computer and use it in GitHub Desktop.
Either in 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
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