mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-12-20 07:37:15 +00:00
either
This commit is contained in:
parent
c4dd19dd00
commit
162294d6c6
@ -2,6 +2,9 @@
|
||||
|
||||
## 0.7.4
|
||||
|
||||
* `Common`:
|
||||
* New type `Either`
|
||||
|
||||
## 0.7.3
|
||||
|
||||
* `Versions`:
|
||||
|
@ -0,0 +1,45 @@
|
||||
package dev.inmo.micro_utils.common
|
||||
|
||||
/**
|
||||
* Realization of this interface will contains at least one not null - [t1] or [t2]
|
||||
*
|
||||
* @see EitherFirst
|
||||
* @see EitherSecond
|
||||
* @see Either.Companion.first
|
||||
* @see Either.Companion.second
|
||||
* @see Either.onFirst
|
||||
* @see Either.onSecond
|
||||
*/
|
||||
sealed interface Either<T1, T2> {
|
||||
val t1: T1?
|
||||
val t2: T2?
|
||||
|
||||
companion object
|
||||
}
|
||||
|
||||
data class EitherFirst<T1, T2>(
|
||||
override val t1: T1
|
||||
) : Either<T1, T2> {
|
||||
override val t2: T2?
|
||||
get() = null
|
||||
}
|
||||
|
||||
data class EitherSecond<T1, T2>(
|
||||
override val t2: T2
|
||||
) : Either<T1, T2> {
|
||||
override val t1: T1?
|
||||
get() = null
|
||||
}
|
||||
|
||||
inline fun <T1, T2> Either.Companion.first(t1: T1): Either<T1, T2> = EitherFirst(t1)
|
||||
inline fun <T1, T2> Either.Companion.second(t1: T1): Either<T1, T2> = EitherFirst(t1)
|
||||
|
||||
inline fun <T1, T2, E : Either<T1, T2>> E.onFirst(crossinline block: (T1) -> Unit): E {
|
||||
if (this is EitherFirst<*, *>) block(t1 as T1)
|
||||
return this
|
||||
}
|
||||
|
||||
inline fun <T1, T2, E : Either<T1, T2>> E.onSecond(crossinline block: (T2) -> Unit): E {
|
||||
if (this is EitherSecond<*, *>) block(t2 as T2)
|
||||
return this
|
||||
}
|
Loading…
Reference in New Issue
Block a user