How to do it...

  1. Declare Either as a sealed class:
sealed class Either<out E, out V>
  1. Add two subclasses of Either, representing Error and Value:
sealed class Either<out L, out R> {
data class Left<out L>(val left: L) : Either<L, Nothing>()
data class Right<out R>(val right: R) : Either<Nothing, R>()
}
  1. Add factory functions for the convenient instantiating of Either:
sealed class Either<out L, out R> {
data class Left<out L>(val left: L) : Either<L, Nothing>()
data class Right<out R>(val right: R) : Either<Nothing, R>()

companion object {
fun <R> right(value: R): Either<Nothing, R> =
Either.Right(value)
fun <L> left(value: L): Either<L, Nothing> =
Either.Left(value)
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.110.152