Skip to content

Commit

Permalink
1.6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
JesusMcCloud committed Jun 10, 2024
1 parent 64d74fc commit c69be73
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@
## 1.6.0
- Kotlin 2.0
- Failure re-throws any fatal and coroutine-related exceptions
- `catching` function, modelling stdlib's `runCatching`, directly returning a `KmmResult`
- `catching` function, modelling stdlib's `runCatching`, directly returning a `KmmResult`

## 1.6.1
- add missing `catching` variant
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ kotlin.mpp.enableCInteropCommonization=true
kotlin.mpp.stability.nowarn=true
kotlin.native.ignoreDisabledTargets=true

artifactVersion = 1.6.0
artifactVersion = 1.6.1
24 changes: 22 additions & 2 deletions src/commonMain/kotlin/at/asitplus/KmmResult.kt
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,28 @@ private constructor(
}

/**
* Non-fatal-only-catching version of stdlib's [runCatching], directly returning a [KmmResult]-
* Non-fatal-only-catching version of stdlib's [runCatching], directly returning a [KmmResult]
* Re-throws any fatal exceptions, such as `OutOfMemoryError`. Relies on [Arrow](https://arrow-kt.io)'s
* [nonFatalOrThrow](https://apidocs.arrow-kt.io/arrow-core/arrow.core/non-fatal-or-throw.html) internally.
*/
inline fun <R> catching(block: () -> R): KmmResult<R> = runCatching { block() }.wrap()
inline fun <reified T> catching(block: () -> T): KmmResult<T> {
return try {
KmmResult.success(block())
} catch (e: Throwable) {
KmmResult.failure(e)
}
}

/**
* Non-fatal-only-catching version of stdlib's [runCatching] (calling the specified function [block] with `this` value
* as its receiver), directly returning a [KmmResult] –
* Re-throws any fatal exceptions, such as `OutOfMemoryError`. Relies on [Arrow](https://arrow-kt.io)'s
* [nonFatalOrThrow](https://apidocs.arrow-kt.io/arrow-core/arrow.core/non-fatal-or-throw.html) internally.
*/
inline fun <T, reified R> T.catching(block: T.() -> R): KmmResult<R> {
return try {
KmmResult.success(block())
} catch (e: Throwable) {
KmmResult.failure(e)
}
}

0 comments on commit c69be73

Please sign in to comment.