Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

orDie combinator #902

Merged
merged 15 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added orDie
  • Loading branch information
johnhungerford committed Dec 5, 2024
commit 58ed90b1ebacb0f51e5de9febe4e7db4db32fff2
21 changes: 21 additions & 0 deletions kyo-combinators/shared/src/main/scala/kyo/Combinators.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import kyo.kernel.Reducible
import scala.annotation.tailrec
import scala.annotation.targetName

sealed class PanicException[A](val error: A) extends Exception(s"Uncaught error: $error")

extension [A, S](effect: A < S)

/** Performs this computation and then the next one, discarding the result of this computation.
Expand Down Expand Up @@ -386,6 +388,25 @@ extension [A, S, E](effect: A < (Abort[E] & S))
): A < (S & reduce.SReduced) =
Abort.run[Throwable](effect.asInstanceOf[A < (Abort[Throwable | ER] & S)])
.map(_.fold(e => throw e.getFailure)(identity))

/** Catches any Aborts and panics instead
*
* @return
* A computation that panics instead of catching Abort effect failures
*/
def orDie(
johnhungerford marked this conversation as resolved.
Show resolved Hide resolved
using
ct: SafeClassTag[E],
tag: Tag[E],
fl: Flat[A],
frame: Frame
): A < S =
Abort.run[E](effect).map(
_.fold(_.getFailure match
case thr: Throwable => throw thr
case other => throw PanicException(other)
)(identity)
)
johnhungerford marked this conversation as resolved.
Show resolved Hide resolved
end extension

extension [A, S, E](effect: A < (Abort[Absent] & S))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,42 @@ class AbortCombinatorTest extends Test:
assert(Abort.run[Any](swapped3).eval == Result.fail(42))
}
}

"orDie" - {
"should remove Abort effects and run successfully if effect has no failures" in {
val effect: Int < Abort[String] =
for
i <- (1: Int < Any)
result <- if i > 1 then Abort.fail("error") else (i: Int < Any)
yield i

val effectOrDie: Int < Any = effect.orDie
assert(effectOrDie.eval == 1)
}

"should remove throwable Abort and throw error" in {
val effect: Int < Abort[Throwable] =
Abort.fail(IndexOutOfBoundsException()).map(_ => 23)

assertThrows[IndexOutOfBoundsException]:
effect.orDie
}

"should remove non-throwable Abort and throw PanicException" in {
val effect: Int < Abort[String] =
Abort.fail("error").map(_ => 23)

try
val result: Int < Any = effect.orDie
assert(???)
catch
case err: PanicException[?] =>
assert(err.error == "error")
case other =>
fail(s"unexpected exception: $other")
end try
}
}
}

end AbortCombinatorTest
Loading