4 ms·
Scala also forces the developer to explore non-happy path cases. But it does so with meaningful abstractions that don't force you to do it on every single line.
by yummyfajitas 10y ago
Scala also forces the developer to explore non-happy path cases. But it does so with meaningful abstractions that don't force you to do it on every single line.
Scala lets you write your code in the style of 'happy path' programming:
val result: MyError \/ ResultType = for {
x <- f(input)
y = g(x)
z <- h(y)
} yield (q(z,y))
Here `g(x)` is a pure function which never has errors. But f(input) and h(y) are impure functions which could return errors.
The error case is handled by `result` being `myError.left[ResultType]`.
To actually access `ResultType`, you use fold:
result.fold(err => ..., r => success(r))
This makes it pretty hard to ignore the error case.