update kdocs of Either

This commit is contained in:
InsanusMokrassar 2021-10-28 18:42:05 +06:00
parent 162294d6c6
commit 8cd0775a6c

View File

@ -17,6 +17,9 @@ sealed interface Either<T1, T2> {
companion object companion object
} }
/**
* This type [Either] will always have not nullable [t1]
*/
data class EitherFirst<T1, T2>( data class EitherFirst<T1, T2>(
override val t1: T1 override val t1: T1
) : Either<T1, T2> { ) : Either<T1, T2> {
@ -24,6 +27,9 @@ data class EitherFirst<T1, T2>(
get() = null get() = null
} }
/**
* This type [Either] will always have not nullable [t2]
*/
data class EitherSecond<T1, T2>( data class EitherSecond<T1, T2>(
override val t2: T2 override val t2: T2
) : Either<T1, T2> { ) : Either<T1, T2> {
@ -31,15 +37,29 @@ data class EitherSecond<T1, T2>(
get() = null get() = null
} }
/**
* @return New instance of [EitherFirst]
*/
inline fun <T1, T2> Either.Companion.first(t1: T1): Either<T1, T2> = EitherFirst(t1) inline fun <T1, T2> Either.Companion.first(t1: T1): Either<T1, T2> = EitherFirst(t1)
/**
* @return New instance of [EitherSecond]
*/
inline fun <T1, T2> Either.Companion.second(t1: T1): Either<T1, T2> = EitherFirst(t1) inline fun <T1, T2> Either.Companion.second(t1: T1): Either<T1, T2> = EitherFirst(t1)
/**
* Will call [block] in case when [Either.t1] of [this] is not null
*/
inline fun <T1, T2, E : Either<T1, T2>> E.onFirst(crossinline block: (T1) -> Unit): E { inline fun <T1, T2, E : Either<T1, T2>> E.onFirst(crossinline block: (T1) -> Unit): E {
if (this is EitherFirst<*, *>) block(t1 as T1) val t1 = t1
t1 ?.let(block)
return this return this
} }
/**
* Will call [block] in case when [Either.t2] of [this] is not null
*/
inline fun <T1, T2, E : Either<T1, T2>> E.onSecond(crossinline block: (T2) -> Unit): E { inline fun <T1, T2, E : Either<T1, T2>> E.onSecond(crossinline block: (T2) -> Unit): E {
if (this is EitherSecond<*, *>) block(t2 as T2) val t2 = t2
t2 ?.let(block)
return this return this
} }