All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.io.github.lyxnx.util.Result.kt Maven / Gradle / Ivy

There is a newer version: 1.6.1
Show newest version
package io.github.lyxnx.util

import kotlin.jvm.JvmSynthetic

/**
 * Maps this result into another result as the result of [block]
 *
 * Note that this function rethrows any [Throwable] exception thrown by [block].
 * See [flatMapCatching] for an alternative that encapsulates exceptions
 */
@JvmSynthetic
public inline fun  Result.flatMap(block: (T) -> Result): Result {
    return map { block(it).getOrThrow() }
}

/**
 * Maps this result into another result as the result of [block]
 *
 * This function catches any [Throwable] exception thrown by [block] and encapsulates it as a failure. See [flatMap] for
 * an alternative that rethrows exceptions
 */
@JvmSynthetic
public inline fun  Result.flatMapCatching(block: (T) -> Result): Result {
    return mapCatching { block(it).getOrThrow() }
}

/**
 * Maps this result into another result as the result of [block], if this result is a failure or the original result if
 * this is a success
 *
 * Note that this function rethrows any [Throwable] exception thrown by [block]. See [recoverCatchingFlatMap] for an
 * alternative that encapsulates exceptions
 */
@JvmSynthetic
public inline fun  Result.recoverFlatMap(block: (Throwable) -> Result): Result {
    return recover { block(it).getOrThrow() }
}

/**
 * Maps this result into another result as the result of [block], if this result is a failure or the original result if
 * this is a success
 *
 * This function catches any [Throwable] exception thrown by [block] and encapsulates it as a failure. See [recoverFlatMap] for
 * an alternative that rethrows exceptions
 */
@JvmSynthetic
public inline fun  Result.recoverCatchingFlatMap(block: (Throwable) -> Result): Result {
    return recoverCatching { block(it).getOrThrow() }
}

/**
 * Returns a result representing a success when only the error type is needed and whether the operation was successful
 *
 * For example, this could be used to represent the result of an operation that returns nothing but may throw an
 * exception; this would allow checking of the state in a safe manner
 */
@JvmSynthetic
public fun Result.Companion.success(): Result = success(Unit)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy