Adding kotlin contract to all functions.
It is now possible to chain consecutive functions which returns Either of the same kind with the try
operator.
object Error
fun String.toInt(): Either<Int, ERROR> = this.toIntOrNull()?.let { Left(it) } ?: Right(ERROR)
fun Int.inBound(range: IntRange): Either<Int, ERROR> = if (this in range) Left(this) else Right(ERROR)
val data: Either<String, ERROR> = Left("123")
val result: Int = data.tryLeft(String::toInt)
.tryLeft { it.inBound(0..<100) }
.requireLeft { return }
assertEquals(123, result)