From 70d0c1a0aef3aaf63c87c7005ac0d0f9f81b2552 Mon Sep 17 00:00:00 2001 From: Lukas Welte Date: Thu, 18 Oct 2018 15:05:33 +0200 Subject: [PATCH] Mark Result, its extensions and runCatching as available since 1.3 --- .../stdlib/coroutines/src/kotlin/Result.kt | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/libraries/stdlib/coroutines/src/kotlin/Result.kt b/libraries/stdlib/coroutines/src/kotlin/Result.kt index 12da51cce12..37bc88533af 100644 --- a/libraries/stdlib/coroutines/src/kotlin/Result.kt +++ b/libraries/stdlib/coroutines/src/kotlin/Result.kt @@ -16,6 +16,7 @@ import kotlin.jvm.JvmField * or a failure with an arbitrary [Throwable] exception. */ @Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS") +@SinceKotlin("1.3") public inline class Result @PublishedApi internal constructor( @PublishedApi internal val value: Any? @@ -108,6 +109,7 @@ public inline class Result @PublishedApi internal constructor( * make sure that this class is not exposed in ABI. */ @PublishedApi +@SinceKotlin("1.3") internal fun createFailure(exception: Throwable): Any = Result.Failure(exception) @@ -117,6 +119,7 @@ internal fun createFailure(exception: Throwable): Any = * add some exception-augmenting logic here (if needed). */ @PublishedApi +@SinceKotlin("1.3") internal fun Result<*>.throwOnFailure() { if (value is Result.Failure) throw value.exception } @@ -126,6 +129,7 @@ internal fun Result<*>.throwOnFailure() { * catching and encapsulating any thrown exception as a failure. */ @InlineOnly +@SinceKotlin("1.3") public inline fun runCatching(block: () -> R): Result { return try { Result.success(block()) @@ -139,6 +143,7 @@ public inline fun runCatching(block: () -> R): Result { * if invocation was successful, catching and encapsulating any thrown exception as a failure. */ @InlineOnly +@SinceKotlin("1.3") public inline fun T.runCatching(block: T.() -> R): Result { return try { Result.success(block()) @@ -156,6 +161,7 @@ public inline fun T.runCatching(block: T.() -> R): Result { * This function is shorthand for `getOrElse { throw it }` (see [getOrElse]). */ @InlineOnly +@SinceKotlin("1.3") public inline fun Result.getOrThrow(): T { throwOnFailure() return value as T @@ -170,6 +176,7 @@ public inline fun Result.getOrThrow(): T { * This function is shorthand for `fold(onSuccess = { it }, onFailure = onFailure)` (see [fold]). */ @InlineOnly +@SinceKotlin("1.3") public inline fun Result.getOrElse(onFailure: (exception: Throwable) -> R): R { contract { callsInPlace(onFailure, InvocationKind.AT_MOST_ONCE) @@ -187,6 +194,7 @@ public inline fun Result.getOrElse(onFailure: (exception: Throwabl * This function is shorthand for `getOrElse { defaultValue }` (see [getOrElse]). */ @InlineOnly +@SinceKotlin("1.3") public inline fun Result.getOrDefault(defaultValue: R): R { if (isFailure) return defaultValue return value as T @@ -199,6 +207,7 @@ public inline fun Result.getOrDefault(defaultValue: R): R { * Note, that an exception thrown by [onSuccess] or by [onFailure] function is rethrown by this function. */ @InlineOnly +@SinceKotlin("1.3") public inline fun Result.fold( onSuccess: (value: T) -> R, onFailure: (exception: Throwable) -> R @@ -224,6 +233,7 @@ public inline fun Result.fold( * See [mapCatching] for an alternative that encapsulates exceptions. */ @InlineOnly +@SinceKotlin("1.3") public inline fun Result.map(transform: (value: T) -> R): Result { contract { callsInPlace(transform, InvocationKind.AT_MOST_ONCE) @@ -243,6 +253,7 @@ public inline fun Result.map(transform: (value: T) -> R): Result { * See [map] for an alternative that rethrows exceptions. */ @InlineOnly +@SinceKotlin("1.3") public inline fun Result.mapCatching(transform: (value: T) -> R): Result { return when { isSuccess -> runCatching { transform(value as T) } @@ -259,6 +270,7 @@ public inline fun Result.mapCatching(transform: (value: T) -> R): Resu * See [recoverCatching] for an alternative that encapsulates exceptions. */ @InlineOnly +@SinceKotlin("1.3") public inline fun Result.recover(transform: (exception: Throwable) -> R): Result { contract { callsInPlace(transform, InvocationKind.AT_MOST_ONCE) @@ -278,6 +290,7 @@ public inline fun Result.recover(transform: (exception: Throwable) * See [recover] for an alternative that rethrows exceptions. */ @InlineOnly +@SinceKotlin("1.3") public inline fun Result.recoverCatching(transform: (exception: Throwable) -> R): Result { val value = value // workaround for inline classes BE bug return when(val exception = exceptionOrNull()) { @@ -293,6 +306,7 @@ public inline fun Result.recoverCatching(transform: (exception: Thr * Returns the original `Result` unchanged. */ @InlineOnly +@SinceKotlin("1.3") public inline fun Result.onFailure(action: (exception: Throwable) -> Unit): Result { contract { callsInPlace(action, InvocationKind.AT_MOST_ONCE) @@ -306,6 +320,7 @@ public inline fun Result.onFailure(action: (exception: Throwable) -> Unit * Returns the original `Result` unchanged. */ @InlineOnly +@SinceKotlin("1.3") public inline fun Result.onSuccess(action: (value: T) -> Unit): Result { contract { callsInPlace(action, InvocationKind.AT_MOST_ONCE) @@ -314,4 +329,4 @@ public inline fun Result.onSuccess(action: (value: T) -> Unit): Result return this } -// ------------------- \ No newline at end of file +// -------------------