From 8cd0775a6c3f526842c4cb30f31fdb3d5deb222c Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 28 Oct 2021 18:42:05 +0600 Subject: [PATCH] update kdocs of Either --- .../dev/inmo/micro_utils/common/Either.kt | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Either.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Either.kt index 20625edbd41..700580015b3 100644 --- a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Either.kt +++ b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Either.kt @@ -17,6 +17,9 @@ sealed interface Either { companion object } +/** + * This type [Either] will always have not nullable [t1] + */ data class EitherFirst( override val t1: T1 ) : Either { @@ -24,6 +27,9 @@ data class EitherFirst( get() = null } +/** + * This type [Either] will always have not nullable [t2] + */ data class EitherSecond( override val t2: T2 ) : Either { @@ -31,15 +37,29 @@ data class EitherSecond( get() = null } +/** + * @return New instance of [EitherFirst] + */ inline fun Either.Companion.first(t1: T1): Either = EitherFirst(t1) +/** + * @return New instance of [EitherSecond] + */ inline fun Either.Companion.second(t1: T1): Either = EitherFirst(t1) +/** + * Will call [block] in case when [Either.t1] of [this] is not null + */ inline fun > E.onFirst(crossinline block: (T1) -> Unit): E { - if (this is EitherFirst<*, *>) block(t1 as T1) + val t1 = t1 + t1 ?.let(block) return this } +/** + * Will call [block] in case when [Either.t2] of [this] is not null + */ inline fun > E.onSecond(crossinline block: (T2) -> Unit): E { - if (this is EitherSecond<*, *>) block(t2 as T2) + val t2 = t2 + t2 ?.let(block) return this }