Skip to content

Commit

Permalink
pacify detekt
Browse files Browse the repository at this point in the history
  • Loading branch information
JesusMcCloud committed Oct 25, 2024
1 parent 62622d3 commit 377d49c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 41 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,8 @@ It re-throws any fatal exceptions, such as `OutOfMemoryError`. The underlying lo
The only downside of `catching` is that it incurs instantiation overhead, because it creates a `KmmResult` instance.
Internally, though, only the behaviour is important, not Swift interop. Hence, you don't care for a `KmmResult` and you
certainly don't care for the cost of instantiating an object. Here, the `Result.nonFatalOrThrow()` extension shipped with KmmResult
comes to the rescue. It does exactly what the name suggest: It re-throws any fatal exception and leaved the `Result` object
untouched otherwise.
In addition, there's `catchingPlain` which directly returns an stdlib `Result`
comes to the rescue. It does exactly what the name suggest: It re-throws any fatal exception and leaves the `Result` object
untouched otherwise. As a convenience shorthand, there's `catchingUnwrapped` which directly returns an stdlib `Result`.

Happy folding!

Expand Down
57 changes: 19 additions & 38 deletions kmmresult/src/commonMain/kotlin/at/asitplus/NonFatalCatching.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
@file:OptIn(kotlin.contracts.ExperimentalContracts::class)
@file:Suppress("TooManyFunctions", "TooGenericExceptionCaught", "MultiLineIfElse")

package at.asitplus

import at.asitplus.KmmResult.Companion.wrap
Expand Down Expand Up @@ -68,14 +70,12 @@ inline fun <T, R> R.catching(block: R.() -> T): KmmResult<T> {

/**
* If the underlying [Result] is successful, returns it unchanged.
* If it is failed, and the contained exception is of the specified type, returns it unchanged.
* If it failed, and the contained exception is of the specified type, returns it unchanged.
* Otherwise, wraps the contained exception in the specified type.
*
* Usage: `Result.wrapAs(a = ::ThrowableType)`
*/
inline fun <reified E: Throwable, T> Result<T>.wrapAs
(a: (String?, Throwable) -> E): Result<T>
{
inline fun <reified E : Throwable, T> Result<T>.wrapAs(a: (String?, Throwable) -> E): Result<T> {
contract { callsInPlace(a, InvocationKind.AT_MOST_ONCE) }
return exceptionOrNull().let { x ->
if ((x == null) || (x is E)) this@wrapAs
Expand All @@ -86,11 +86,9 @@ inline fun <reified E: Throwable, T> Result<T>.wrapAs
/** @see wrapAs */
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
@kotlin.internal.LowPriorityInOverloadResolution
inline fun <reified E: Throwable, R> Result<R>.wrapAs
(a: (Throwable) -> E) : Result<R>
{
inline fun <reified E : Throwable, R> Result<R>.wrapAs(a: (Throwable) -> E): Result<R> {
contract { callsInPlace(a, InvocationKind.AT_MOST_ONCE) }
return wrapAs(a={ _,x -> a(x) })
return wrapAs(a = { _, x -> a(x) })
}

/**
Expand All @@ -99,9 +97,7 @@ inline fun <reified E: Throwable, R> Result<R>.wrapAs
*
* Usage: `catchingUnwrappedAs(type = ::ThrowableType) { block }`.
*/
inline fun <reified E : Throwable, T> catchingUnwrappedAs
(a: (String?, Throwable) -> E, block: () -> T): Result<T>
{
inline fun <reified E : Throwable, T> catchingUnwrappedAs(a: (String?, Throwable) -> E, block: () -> T): Result<T> {
contract {
callsInPlace(a, InvocationKind.AT_MOST_ONCE)
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
Expand All @@ -110,9 +106,10 @@ inline fun <reified E : Throwable, T> catchingUnwrappedAs
}

/** @see catchingUnwrappedAs */
inline fun <reified E : Throwable, T, R> R.catchingUnwrappedAs
(a: (String?, Throwable) -> E, block: R.() -> T): Result<T>
{
inline fun <reified E : Throwable, T, R> R.catchingUnwrappedAs(
a: (String?, Throwable) -> E,
block: R.() -> T
): Result<T> {
contract {
callsInPlace(a, InvocationKind.AT_MOST_ONCE)
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
Expand All @@ -123,9 +120,7 @@ inline fun <reified E : Throwable, T, R> R.catchingUnwrappedAs
/** @see catchingUnwrappedAs */
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
@kotlin.internal.LowPriorityInOverloadResolution
inline fun <reified E : Throwable, T> catchingUnwrappedAs
(a: (Throwable) -> E, block: () -> T): Result<T>
{
inline fun <reified E : Throwable, T> catchingUnwrappedAs(a: (Throwable) -> E, block: () -> T): Result<T> {
contract {
callsInPlace(a, InvocationKind.AT_MOST_ONCE)
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
Expand All @@ -136,9 +131,7 @@ inline fun <reified E : Throwable, T> catchingUnwrappedAs
/** @see catchingUnwrappedAs */
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
@kotlin.internal.LowPriorityInOverloadResolution
inline fun <reified E : Throwable, T, R> R.catchingUnwrappedAs
(a: (Throwable) -> E, block: R.() -> T): Result<T>
{
inline fun <reified E : Throwable, T, R> R.catchingUnwrappedAs(a: (Throwable) -> E, block: R.() -> T): Result<T> {
contract {
callsInPlace(a, InvocationKind.AT_MOST_ONCE)
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
Expand All @@ -152,9 +145,7 @@ inline fun <reified E : Throwable, T, R> R.catchingUnwrappedAs
*
* Usage: `catchingAs(a = ::ThrowableType) { block }`.
*/
inline fun <reified E : Throwable, T> catchingAs
(a: (String?, Throwable) -> E, block: () -> T): KmmResult<T>
{
inline fun <reified E : Throwable, T> catchingAs(a: (String?, Throwable) -> E, block: () -> T): KmmResult<T> {
contract {
callsInPlace(a, InvocationKind.AT_MOST_ONCE)
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
Expand All @@ -163,9 +154,7 @@ inline fun <reified E : Throwable, T> catchingAs
}

/** @see catchingAs */
inline fun <reified E : Throwable, T, R> R.catchingAs
(a: (String?, Throwable) -> E, block: R.() -> T): KmmResult<T>
{
inline fun <reified E : Throwable, T, R> R.catchingAs(a: (String?, Throwable) -> E, block: R.() -> T): KmmResult<T> {
contract {
callsInPlace(a, InvocationKind.AT_MOST_ONCE)
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
Expand All @@ -176,9 +165,7 @@ inline fun <reified E : Throwable, T, R> R.catchingAs
/** @see catchingAs */
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
@kotlin.internal.LowPriorityInOverloadResolution
inline fun <reified E : Throwable, T> catchingAs
(a: (Throwable) -> E, block: () -> T): KmmResult<T>
{
inline fun <reified E : Throwable, T> catchingAs(a: (Throwable) -> E, block: () -> T): KmmResult<T> {
contract {
callsInPlace(a, InvocationKind.AT_MOST_ONCE)
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
Expand All @@ -188,9 +175,7 @@ inline fun <reified E : Throwable, T> catchingAs

@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
@kotlin.internal.LowPriorityInOverloadResolution
inline fun <reified E: Throwable, T, R> R.catchingAs
(a: (Throwable) -> E, block: R.() -> T): KmmResult<T>
{
inline fun <reified E : Throwable, T, R> R.catchingAs(a: (Throwable) -> E, block: R.() -> T): KmmResult<T> {
contract {
callsInPlace(a, InvocationKind.AT_MOST_ONCE)
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
Expand All @@ -199,9 +184,7 @@ inline fun <reified E: Throwable, T, R> R.catchingAs
}

@Deprecated("Function name was misleading", ReplaceWith("catchingAs(asA, block)"))
inline fun <reified E : Throwable, R> wrapping
(asA: (String?, Throwable) -> E, block: () -> R): KmmResult<R>
{
inline fun <reified E : Throwable, R> wrapping(asA: (String?, Throwable) -> E, block: () -> R): KmmResult<R> {
contract {
callsInPlace(asA, InvocationKind.AT_MOST_ONCE)
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
Expand All @@ -210,9 +193,7 @@ inline fun <reified E : Throwable, R> wrapping
}

@Deprecated("Function name was misleading", ReplaceWith("catchingAs(asA, block)"))
inline fun <reified E : Throwable, T, R> R.wrapping
(asA: (String?, Throwable) -> E, block: R.() -> T): KmmResult<T>
{
inline fun <reified E : Throwable, T, R> R.wrapping(asA: (String?, Throwable) -> E, block: R.() -> T): KmmResult<T> {
contract {
callsInPlace(asA, InvocationKind.AT_MOST_ONCE)
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
Expand Down

0 comments on commit 377d49c

Please sign in to comment.