Working with Either
"The Either type represents values with two possibilities: a value of type Either<'L,'R> is either Left<'L> or Right<'R>. The Either type is sometimes used to represent a value which is either correct or an error; by convention, Left is used to hold an error value and Right holds a correct value" - Haskell documentation
Either instance
Create an Either by injecting a value into either case with Either.Left<'L,'R>('L) or Either.Right<'L,'R>('R).
1: 2: |
|
Use the FromEither<'L,'R,'T>('L->'T,'R->'T) function to extract the value from either side.
1: 2: |
|
Check out the API Reference for more information.
Exception and error-handling
The following snippet depicts a three step process to load, validate and launch a saved game. Each of these steps can potentially fail and raise an Exception that needs to be caught.
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: |
|
Composing Either
Using the monadic bind function of Either we can compose these steps together while preserving granular information like individual error messages.
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: |
|