sequence
expressions
in Mint
A sequence
expression, in Mint, is how you chain operations
returning success or failure.
Here are some takeaways that I didn’t get from the documentation the first time around:
The body of a
sequence
is the happy path
It’s not explicitly stated, but the main body of a
sequence
is the “happy” path through the code, i.e. all
operations succeed. Thus the left-hand-side values in the assignments in
your sequence
aren’t the actual error types—they’re the
unwrapped “success” values.
Errors land you in one of the catch
blocks below,
somewhat like an exception handler.
Don’t use empty catch
blocks
So how do you verify that you’re catching all the right errors? The
compiler will tell you, if you let it. Avoid the temptation to use an
empty catch
block, and start with none. Then, add
catch
blocks one at a time until you’ve cleared the
compiler errors.
Wrap arbitrary error types
in Result
A sequence
knows how to unwrap values from a
Promise
or a Result
, and that’s it. But there
are library functions that return something like a Maybe
,
or a String, to signal an error—for example,
Json.parse
.
To use these functions in a sequence
, you’ll need to
wrap their return types in a Result
, using functions such
as Maybe.toResult
.