July 2018
Intermediate to advanced
242 pages
8h 6m
English
In order to make use of the class Either and benefit from the Either.right() and Either.left() methods, we can implement a getEither() function that will try to perform some operation passed to it as a parameter. If the operation succeeds, it is going to return the Either.Right instance holding the result of the operation, otherwise, it is going to return Either.Left, holding a thrown exception instance:
fun <V> getEither(action: () -> V): Either<Exception, V> = try { Either.right(action()) } catch (e: Exception) { Either.left(e) }
By convention, we use the Either.Right type to provide a default value and Either.Left to handle any possible edge cases.
Read now
Unlock full access