Skip to content

Version 1.2.1

Latest
Compare
Choose a tag to compare
@L-Briand L-Briand released this 09 Feb 09:58
· 2 commits to main since this release

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)