
Represents a result type encompassing success or failure, offering customizable failure types, and features like `andThen`, `whenFailure`, and a `fold` method for pattern-matching-like functionality.
A Result type representing either a success or a failure.
This is a Kotlin multi-platform library, supporting JVM, JS and major native targets. If you're a Kotlin JVM user, just add the following to the dependencies.
implementation("me.jason5lee:resukt:1.0.0")
implementation("me.jason5lee:resukt-jvm:1.0.0")Unlike other result libraries, this library is implemented based on the one in the standard library, with the following opinionated designs.
Result<T, F> instead of Result<T>.Throwable. Actually I don't recommend defining failure as a Throwable.
The failures of the logic are very different from the exceptions from the implementation and IO.Throwable-related functions are put in the throwable package
which I don't recommend to use.andThen and whenFailure methods for easy error spreading in a multi-steps procedure. Check MultiStepsTest.kt for examples.Result<T> in standard library.For the people from pattern-matching or smart-casting languages: because it does not use the sealed class, it provides
a fold method having the similar function of pattern-matching. It accepts two lambdas expression, one for the success
case and another for failure case. For example, for a a: Result<Int, String>, you can do something like
a.fold(onSuccess = { it / 2 }, onFailure = { println("Error: $it"); -1 }) .
Because Kotlin's standard library will be used if this Result is not imported, if you get a strange compile
error, be sure you have me.jason5lee.resukt.Result or me.jason5lee.resukt.throwable.runCatching imported.
A Result type representing either a success or a failure.
This is a Kotlin multi-platform library, supporting JVM, JS and major native targets. If you're a Kotlin JVM user, just add the following to the dependencies.
implementation("me.jason5lee:resukt:1.0.0")
implementation("me.jason5lee:resukt-jvm:1.0.0")Unlike other result libraries, this library is implemented based on the one in the standard library, with the following opinionated designs.
Result<T, F> instead of Result<T>.Throwable. Actually I don't recommend defining failure as a Throwable.
The failures of the logic are very different from the exceptions from the implementation and IO.Throwable-related functions are put in the throwable package
which I don't recommend to use.andThen and whenFailure methods for easy error spreading in a multi-steps procedure. Check MultiStepsTest.kt for examples.Result<T> in standard library.For the people from pattern-matching or smart-casting languages: because it does not use the sealed class, it provides
a fold method having the similar function of pattern-matching. It accepts two lambdas expression, one for the success
case and another for failure case. For example, for a a: Result<Int, String>, you can do something like
a.fold(onSuccess = { it / 2 }, onFailure = { println("Error: $it"); -1 }) .
Because Kotlin's standard library will be used if this Result is not imported, if you get a strange compile
error, be sure you have me.jason5lee.resukt.Result or me.jason5lee.resukt.throwable.runCatching imported.