Mark Result, its extensions and runCatching as available since 1.3

This commit is contained in:
Lukas Welte
2018-10-18 15:05:33 +02:00
committed by ilya-g
parent 374eec04d4
commit 70d0c1a0ae
@@ -16,6 +16,7 @@ import kotlin.jvm.JvmField
* or a failure with an arbitrary [Throwable] exception. * or a failure with an arbitrary [Throwable] exception.
*/ */
@Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS") @Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS")
@SinceKotlin("1.3")
public inline class Result<out T> @PublishedApi internal constructor( public inline class Result<out T> @PublishedApi internal constructor(
@PublishedApi @PublishedApi
internal val value: Any? internal val value: Any?
@@ -108,6 +109,7 @@ public inline class Result<out T> @PublishedApi internal constructor(
* make sure that this class is not exposed in ABI. * make sure that this class is not exposed in ABI.
*/ */
@PublishedApi @PublishedApi
@SinceKotlin("1.3")
internal fun createFailure(exception: Throwable): Any = internal fun createFailure(exception: Throwable): Any =
Result.Failure(exception) Result.Failure(exception)
@@ -117,6 +119,7 @@ internal fun createFailure(exception: Throwable): Any =
* add some exception-augmenting logic here (if needed). * add some exception-augmenting logic here (if needed).
*/ */
@PublishedApi @PublishedApi
@SinceKotlin("1.3")
internal fun Result<*>.throwOnFailure() { internal fun Result<*>.throwOnFailure() {
if (value is Result.Failure) throw value.exception 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. * catching and encapsulating any thrown exception as a failure.
*/ */
@InlineOnly @InlineOnly
@SinceKotlin("1.3")
public inline fun <R> runCatching(block: () -> R): Result<R> { public inline fun <R> runCatching(block: () -> R): Result<R> {
return try { return try {
Result.success(block()) Result.success(block())
@@ -139,6 +143,7 @@ public inline fun <R> runCatching(block: () -> R): Result<R> {
* if invocation was successful, catching and encapsulating any thrown exception as a failure. * if invocation was successful, catching and encapsulating any thrown exception as a failure.
*/ */
@InlineOnly @InlineOnly
@SinceKotlin("1.3")
public inline fun <T, R> T.runCatching(block: T.() -> R): Result<R> { public inline fun <T, R> T.runCatching(block: T.() -> R): Result<R> {
return try { return try {
Result.success(block()) Result.success(block())
@@ -156,6 +161,7 @@ public inline fun <T, R> T.runCatching(block: T.() -> R): Result<R> {
* This function is shorthand for `getOrElse { throw it }` (see [getOrElse]). * This function is shorthand for `getOrElse { throw it }` (see [getOrElse]).
*/ */
@InlineOnly @InlineOnly
@SinceKotlin("1.3")
public inline fun <T> Result<T>.getOrThrow(): T { public inline fun <T> Result<T>.getOrThrow(): T {
throwOnFailure() throwOnFailure()
return value as T return value as T
@@ -170,6 +176,7 @@ public inline fun <T> Result<T>.getOrThrow(): T {
* This function is shorthand for `fold(onSuccess = { it }, onFailure = onFailure)` (see [fold]). * This function is shorthand for `fold(onSuccess = { it }, onFailure = onFailure)` (see [fold]).
*/ */
@InlineOnly @InlineOnly
@SinceKotlin("1.3")
public inline fun <R, T : R> Result<T>.getOrElse(onFailure: (exception: Throwable) -> R): R { public inline fun <R, T : R> Result<T>.getOrElse(onFailure: (exception: Throwable) -> R): R {
contract { contract {
callsInPlace(onFailure, InvocationKind.AT_MOST_ONCE) callsInPlace(onFailure, InvocationKind.AT_MOST_ONCE)
@@ -187,6 +194,7 @@ public inline fun <R, T : R> Result<T>.getOrElse(onFailure: (exception: Throwabl
* This function is shorthand for `getOrElse { defaultValue }` (see [getOrElse]). * This function is shorthand for `getOrElse { defaultValue }` (see [getOrElse]).
*/ */
@InlineOnly @InlineOnly
@SinceKotlin("1.3")
public inline fun <R, T : R> Result<T>.getOrDefault(defaultValue: R): R { public inline fun <R, T : R> Result<T>.getOrDefault(defaultValue: R): R {
if (isFailure) return defaultValue if (isFailure) return defaultValue
return value as T return value as T
@@ -199,6 +207,7 @@ public inline fun <R, T : R> Result<T>.getOrDefault(defaultValue: R): R {
* Note, that an exception thrown by [onSuccess] or by [onFailure] function is rethrown by this function. * Note, that an exception thrown by [onSuccess] or by [onFailure] function is rethrown by this function.
*/ */
@InlineOnly @InlineOnly
@SinceKotlin("1.3")
public inline fun <R, T> Result<T>.fold( public inline fun <R, T> Result<T>.fold(
onSuccess: (value: T) -> R, onSuccess: (value: T) -> R,
onFailure: (exception: Throwable) -> R onFailure: (exception: Throwable) -> R
@@ -224,6 +233,7 @@ public inline fun <R, T> Result<T>.fold(
* See [mapCatching] for an alternative that encapsulates exceptions. * See [mapCatching] for an alternative that encapsulates exceptions.
*/ */
@InlineOnly @InlineOnly
@SinceKotlin("1.3")
public inline fun <R, T> Result<T>.map(transform: (value: T) -> R): Result<R> { public inline fun <R, T> Result<T>.map(transform: (value: T) -> R): Result<R> {
contract { contract {
callsInPlace(transform, InvocationKind.AT_MOST_ONCE) callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
@@ -243,6 +253,7 @@ public inline fun <R, T> Result<T>.map(transform: (value: T) -> R): Result<R> {
* See [map] for an alternative that rethrows exceptions. * See [map] for an alternative that rethrows exceptions.
*/ */
@InlineOnly @InlineOnly
@SinceKotlin("1.3")
public inline fun <R, T> Result<T>.mapCatching(transform: (value: T) -> R): Result<R> { public inline fun <R, T> Result<T>.mapCatching(transform: (value: T) -> R): Result<R> {
return when { return when {
isSuccess -> runCatching { transform(value as T) } isSuccess -> runCatching { transform(value as T) }
@@ -259,6 +270,7 @@ public inline fun <R, T> Result<T>.mapCatching(transform: (value: T) -> R): Resu
* See [recoverCatching] for an alternative that encapsulates exceptions. * See [recoverCatching] for an alternative that encapsulates exceptions.
*/ */
@InlineOnly @InlineOnly
@SinceKotlin("1.3")
public inline fun <R, T: R> Result<T>.recover(transform: (exception: Throwable) -> R): Result<R> { public inline fun <R, T: R> Result<T>.recover(transform: (exception: Throwable) -> R): Result<R> {
contract { contract {
callsInPlace(transform, InvocationKind.AT_MOST_ONCE) callsInPlace(transform, InvocationKind.AT_MOST_ONCE)
@@ -278,6 +290,7 @@ public inline fun <R, T: R> Result<T>.recover(transform: (exception: Throwable)
* See [recover] for an alternative that rethrows exceptions. * See [recover] for an alternative that rethrows exceptions.
*/ */
@InlineOnly @InlineOnly
@SinceKotlin("1.3")
public inline fun <R, T: R> Result<T>.recoverCatching(transform: (exception: Throwable) -> R): Result<R> { public inline fun <R, T: R> Result<T>.recoverCatching(transform: (exception: Throwable) -> R): Result<R> {
val value = value // workaround for inline classes BE bug val value = value // workaround for inline classes BE bug
return when(val exception = exceptionOrNull()) { return when(val exception = exceptionOrNull()) {
@@ -293,6 +306,7 @@ public inline fun <R, T: R> Result<T>.recoverCatching(transform: (exception: Thr
* Returns the original `Result` unchanged. * Returns the original `Result` unchanged.
*/ */
@InlineOnly @InlineOnly
@SinceKotlin("1.3")
public inline fun <T> Result<T>.onFailure(action: (exception: Throwable) -> Unit): Result<T> { public inline fun <T> Result<T>.onFailure(action: (exception: Throwable) -> Unit): Result<T> {
contract { contract {
callsInPlace(action, InvocationKind.AT_MOST_ONCE) callsInPlace(action, InvocationKind.AT_MOST_ONCE)
@@ -306,6 +320,7 @@ public inline fun <T> Result<T>.onFailure(action: (exception: Throwable) -> Unit
* Returns the original `Result` unchanged. * Returns the original `Result` unchanged.
*/ */
@InlineOnly @InlineOnly
@SinceKotlin("1.3")
public inline fun <T> Result<T>.onSuccess(action: (value: T) -> Unit): Result<T> { public inline fun <T> Result<T>.onSuccess(action: (value: T) -> Unit): Result<T> {
contract { contract {
callsInPlace(action, InvocationKind.AT_MOST_ONCE) callsInPlace(action, InvocationKind.AT_MOST_ONCE)
@@ -314,4 +329,4 @@ public inline fun <T> Result<T>.onSuccess(action: (value: T) -> Unit): Result<T>
return this return this
} }
// ------------------- // -------------------