OR
Introducing Either Monad in Groovy
By - Prakash Balodi
Software Engineer @ Quintype Inc.
Prakash Balodi
Error Handling is always an implicit requirement.
Can be the difference between Good code and bad code.
Lot of conditionals (switch)
Especially ugly if method calls are large chains
var i=method1()
if(!i){
return;
}
var j=function2();
if(!j){
return;
}
.
.
.
Or We can use exceptions!
But Exceptions are -
Not Local Solutions
Can leak abstractions!
Functions are no longer pure!
- The Internet
Can we do better?
The Imperative Model for Error Handling
The Railway Model for Error Handling
Note that we always move forward!
With Railway oriented Model, we write our functions as -
so every function can either return a success value or a failure value.
=
Now what we need is some way to connect these functions
And then we can join these functions -
We can add as many joins as we want!
But How do we Model Both Success and Failure coming out of a function?
What's Either?
What's Either?
And How do we connect multiple functions?
Either has a bind function!
Railway Oriented Programming - https://www.youtube.com/watch?v=UvD1VjRvGIk