Skip to content

Commit

Permalink
Updated README
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhungerford committed Feb 1, 2025
1 parent fdc8641 commit 6b569a2
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3504,16 +3504,19 @@ val choiceEffect: Int < Choice = maybeEffect.absentToEmpty
val newAbortEffect: Int < (Choice & Abort[Throwable]) = choiceEffect.emptyToThrowable
```

To swallow errors à la ZIO's `orDie` and `resurrect` methods, you can use `orPanic` and `unpanic` respectively:
To swallow errors à la ZIO's `orDie` and `resurrect` methods, you can use `orPanic`/`orThrow` and `unpanic` respectively:

```scala
import kyo.*
import java.io.IOException

val abortEffect: Int < Abort[String | Throwable] = 1

// unsafeEffect will panic with a `PanicException(err)`
val unsafeEffect: Int < Any = abortEffect.orPanic
// Will panic with a `PanicException(err)` rather than fail
val panicEffect: Int < Abort[Nothing] = abortEffect.orPanic

// Will throw `PanicException(err)` when evaluated
val unsafeEffect: Int < Any = abortEffect.orThrow

// Catch any suspended throws
val safeEffect: Int < Abort[Throwable] = unsafeEffect.unpanic
Expand All @@ -3522,6 +3525,8 @@ val safeEffect: Int < Abort[Throwable] = unsafeEffect.unpanic
val unsafeForThrowables: Int < Abort[String] = abortEffect.forAbort[Throwable].orPanic
```

In general `orPanic` should be preferred over `orThrow`, especially when used in conjunction with `IO` or `Async`, both of which include `Abort[Nothing]`. This will avoid unnecessary catching/re-throwing.

Other error-handling methods are as follows:

```scala
Expand All @@ -3534,9 +3539,11 @@ trait C
val effect: Int < Abort[A | B | C] = 1

val handled: Result[A | B | C, Int] < Any = effect.result
val handledWithoutPanic: Result.Partial[A | B | C, Int] < Any = effect.partialResult
val handledWithoutPanic: Result.Partial[A | B | C, Int] < Abort[Nothing] = effect.partialResult
val unsafeHandled: Result.Partial[A | B | C, Int] < Any = effect.partialResultOrThrow
val folded: String < Any = effect.foldAbort(_.toString, _.toString, _.toString)
val foldedWithoutPanic: String < Any = effect.foldAbort(_.toString, _.toString)
val foldedWithoutPanic: String < Abort[Nothing] = effect.foldAbort(_.toString, _.toString)
val unsafeFolded: String < Any = effect.foldAbortOrThrow(_.toString, _.toString)
val mappedError: Int < Abort[String] = effect.mapAbort(_.toString)
val caught: Int < Any = effect.catching(_.toString.size)
val partiallyCaught: Int < Abort[A | B | C] = effect.catchingSome { case err if err.toString.size > 5 => 0 }
Expand Down

0 comments on commit 6b569a2

Please sign in to comment.