diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Optional.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Optional.kt index a166434beb7..3c48c1b4da9 100644 --- a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Optional.kt +++ b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Optional.kt @@ -1,3 +1,5 @@ +@file:Suppress("unused") + package dev.inmo.micro_utils.common import kotlinx.serialization.Serializable @@ -41,7 +43,7 @@ inline val T.optional * Will call [block] when data presented ([Optional.dataPresented] == true) */ fun Optional.onPresented(block: (T) -> Unit): Optional = apply { - if (dataPresented) { block(data as T) } + if (dataPresented) { @Suppress("UNCHECKED_CAST") block(data as T) } } /** @@ -54,20 +56,20 @@ fun Optional.onAbsent(block: () -> Unit): Optional = apply { /** * Returns [Optional.data] if [Optional.dataPresented] of [this] is true, or null otherwise */ -fun Optional.dataOrNull() = if (dataPresented) data as T else null +fun Optional.dataOrNull() = if (dataPresented) @Suppress("UNCHECKED_CAST") (data as T) else null /** * Returns [Optional.data] if [Optional.dataPresented] of [this] is true, or throw [throwable] otherwise */ -fun Optional.dataOrThrow(throwable: Throwable) = if (dataPresented) data as T else throw throwable +fun Optional.dataOrThrow(throwable: Throwable) = if (dataPresented) @Suppress("UNCHECKED_CAST") (data as T) else throw throwable /** * Returns [Optional.data] if [Optional.dataPresented] of [this] is true, or call [block] and returns the result of it */ -fun Optional.dataOrElse(block: () -> T) = if (dataPresented) data as T else block() +fun Optional.dataOrElse(block: () -> T) = if (dataPresented) @Suppress("UNCHECKED_CAST") (data as T) else block() /** * Returns [Optional.data] if [Optional.dataPresented] of [this] is true, or call [block] and returns the result of it */ -suspend fun Optional.dataOrElseSuspendable(block: suspend () -> T) = if (dataPresented) data as T else block() +suspend fun Optional.dataOrElseSuspendable(block: suspend () -> T) = if (dataPresented) @Suppress("UNCHECKED_CAST") (data as T) else block()