This enables a very idiomatic way of error handling: assume you are trying to read 42 bytes from a file into a string. Let open be a feature that takes a file name and returns an outcome of some abstract file descriptor, let read be a feature that takes an abstract file descriptor and a number of bytes to read and returns an outcome of a byte (u8) array. We want to read 42 bytes from a file called "example.txt" into a string, and we wrap this into an outcome, for handling the case that an error occurs when opening the file (i.e. it does not exist) or when reading the file (i.e. example.txt is not actually a file but a directory). Using match expressions, this is very cumbersome:
This enables a very idiomatic way of error handling: assume you are trying
to read 42 bytes from a file into a string. Let open be a feature that takes
a file name and returns an outcome of some abstract file descriptor, let read
be a feature that takes an abstract file descriptor and a number of bytes to
read and returns an outcome of a byte (u8) array. We want to read 42 bytes
from a file called "example.txt" into a string, and we wrap this into an outcome,
for handling the case that an error occurs when opening the file (i.e. it does
not exist) or when reading the file (i.e. example.txt is not actually a file but
a directory). Using match expressions, this is very cumbersome:
The monadic operator turns this into: