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.
*/
@Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS")
@SinceKotlin("1.3")
public inline class Result<out T> @PublishedApi internal constructor(
@PublishedApi
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.
*/
@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 <R> runCatching(block: () -> R): Result<R> {
return try {
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.
*/
@InlineOnly
@SinceKotlin("1.3")
public inline fun <T, R> T.runCatching(block: T.() -> R): Result<R> {
return try {
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]).
*/
@InlineOnly
@SinceKotlin("1.3")
public inline fun <T> Result<T>.getOrThrow(): T {
throwOnFailure()
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]).
*/
@InlineOnly
@SinceKotlin("1.3")
public inline fun <R, T : R> Result<T>.getOrElse(onFailure: (exception: Throwable) -> R): R {
contract {
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]).
*/
@InlineOnly
@SinceKotlin("1.3")
public inline fun <R, T : R> Result<T>.getOrDefault(defaultValue: R): R {
if (isFailure) return defaultValue
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.
*/
@InlineOnly
@SinceKotlin("1.3")
public inline fun <R, T> Result<T>.fold(
onSuccess: (value: T) -> 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.
*/
@InlineOnly
@SinceKotlin("1.3")
public inline fun <R, T> Result<T>.map(transform: (value: T) -> R): Result<R> {
contract {
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.
*/
@InlineOnly
@SinceKotlin("1.3")
public inline fun <R, T> Result<T>.mapCatching(transform: (value: T) -> R): Result<R> {
return when {
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.
*/
@InlineOnly
@SinceKotlin("1.3")
public inline fun <R, T: R> Result<T>.recover(transform: (exception: Throwable) -> R): Result<R> {
contract {
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.
*/
@InlineOnly
@SinceKotlin("1.3")
public inline fun <R, T: R> Result<T>.recoverCatching(transform: (exception: Throwable) -> R): Result<R> {
val value = value // workaround for inline classes BE bug
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.
*/
@InlineOnly
@SinceKotlin("1.3")
public inline fun <T> Result<T>.onFailure(action: (exception: Throwable) -> Unit): Result<T> {
contract {
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.
*/
@InlineOnly
@SinceKotlin("1.3")
public inline fun <T> Result<T>.onSuccess(action: (value: T) -> Unit): Result<T> {
contract {
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
}
// -------------------
// -------------------