Skip to content

Commit

Permalink
Change return type of raise to Nothing
Browse files Browse the repository at this point in the history
Implements arrow-kt#2805
  • Loading branch information
roomscape committed Oct 10, 2022
1 parent c5e122d commit 33bfa6b
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public inline fun <E, A> ior(semigroup: Semigroup<E>, @BuilderInference action:
public value class NullableRaise(private val cont: Raise<Nothing?>) : Raise<Nothing?> {
@EffectDSL
public fun ensure(value: Boolean): Unit = ensure(value) { null }
override fun <B> raise(r: Nothing?): B = cont.raise(r)
override fun raise(r: Nothing?): Nothing = cont.raise(r)
public fun <B> Option<B>.bind(): B = bind { raise(null) }

public fun <B> B?.bind(): B {
Expand All @@ -54,13 +54,13 @@ public value class NullableRaise(private val cont: Raise<Nothing?>) : Raise<Noth

@JvmInline
public value class ResultRaise(private val cont: Raise<Throwable>) : Raise<Throwable> {
override fun <B> raise(r: Throwable): B = cont.raise(r)
override fun raise(r: Throwable): Nothing = cont.raise(r)
public fun <B> Result<B>.bind(): B = fold(::identity) { raise(it) }
}

@JvmInline
public value class OptionRaise(private val cont: Raise<None>) : Raise<None> {
override fun <B> raise(r: None): B = cont.raise(r)
override fun raise(r: None): Nothing = cont.raise(r)
public fun <B> Option<B>.bind(): B = bind { raise(None) }
public fun ensure(value: Boolean): Unit = ensure(value) { None }

Expand All @@ -76,7 +76,7 @@ public class IorRaise<E> @PublishedApi internal constructor(semigroup: Semigroup
// TODO this is a mess...
@PublishedApi
internal var leftState: AtomicRef<Any?> = AtomicRef(EmptyValue)
override fun <B> raise(r: E): B = effect.raise(combine(r))
override fun raise(r: E): Nothing = effect.raise(combine(r))

public fun <B> Ior<E, B>.bind(): B =
when (this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ import kotlin.jvm.JvmMultifileClass
* val error = "Error"
* val exit = CompletableDeferred<ExitCase>()
* effect<String, Int> {
* parZip({ awaitExitCase<Int>(exit) }, { raise<Int>(error) }) { a, b -> a + b }
* parZip({ awaitExitCase<Int>(exit) }, { raise(error) }) { a: Int, b: Int -> a + b }
* }.fold({ it shouldBe error }, { fail("Int can never be the result") })
* exit.await().shouldBeTypeOf<ExitCase>()
* }
Expand Down Expand Up @@ -425,7 +425,7 @@ import kotlin.jvm.JvmMultifileClass
* val error = "Error"
* val exit = CompletableDeferred<ExitCase>()
* effect<String, Int> {
* raceN({ awaitExitCase<Int>(exit) }) { raise<Int>(error) }
* raceN({ awaitExitCase<Int>(exit) }) { raise(error) }
* .merge() // Flatten Either<Int, Int> result from race into Int
* }.fold({ msg -> msg shouldBe error }, { fail("Int can never be the result") })
* // It's possible not all parallel task got launched, and in those cases awaitCancellation never ran
Expand Down Expand Up @@ -497,7 +497,7 @@ import kotlin.jvm.JvmMultifileClass
* resourceScope {
* effect<String, Int> {
* val reader = bufferedReader("build.gradle.kts")
* raise<Int>(error)
* raise(error)
* reader.lineSequence().count()
* }.fold({ it shouldBe error }, { fail("Int can never be the result") })
* }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ internal fun <R> CancellationException.raisedOrRethrow(raise: DefaultRaise): R =
/** Serves as both purposes of a scope-reference token, and a default implementation for Raise. */
@PublishedApi
internal class DefaultRaise : Raise<Any?> {
override fun <B> raise(r: Any?): B = throw RaiseCancellationException(r, this)
override fun raise(r: Any?): Nothing = throw RaiseCancellationException(r, this)
}

/** CancellationException is required to cancel coroutines when shifting from within them. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public annotation class EffectDSL
public interface Raise<in R> {

/** Raise a _logical failure_ of type [R] */
public fun <A> raise(r: R): A
public fun raise(r: R): Nothing

/**
* Invoke an [EagerEffect] inside `this` [Raise] context.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class EagerEffectSpec : StringSpec({
val promise = CompletableDeferred<Int>()
eagerEffect {
try {
raise<Int>(s)
raise(s)
} finally {
require(promise.complete(i))
}
Expand Down Expand Up @@ -144,7 +144,7 @@ class EagerEffectSpec : StringSpec({
"catch - error path and recover" {
checkAll(Arb.int(), Arb.string()) { int, fallback ->
eagerEffect<Int, String> {
raise<String>(int)
raise(int)
fail("It should never reach this point")
}.recover<Int, Nothing, String> { fallback }
.runCont() shouldBe fallback
Expand All @@ -154,7 +154,7 @@ class EagerEffectSpec : StringSpec({
"catch - error path and re-raise" {
checkAll(Arb.int(), Arb.string()) { int, fallback ->
eagerEffect<Int, Unit> {
raise<String>(int)
raise(int)
fail("It should never reach this point")
}.recover { raise(fallback) }
.runCont() shouldBe fallback
Expand All @@ -165,7 +165,7 @@ class EagerEffectSpec : StringSpec({
checkAll(Arb.int(), Arb.string()) { int, msg ->
shouldThrow<RuntimeException> {
eagerEffect<Int, String> {
raise<String>(int)
raise(int)
fail("It should never reach this point")
}.recover<Int, Nothing, String> { throw RuntimeException(msg) }
.runCont()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class EffectSpec :
val promise = CompletableDeferred<Int>()
effect {
try {
raise<Int>(s().suspend())
raise(s().suspend())
} finally {
require(promise.complete(i()))
}
Expand Down Expand Up @@ -132,7 +132,7 @@ class EffectSpec :
"short-circuit" {
checkAll(Arb.string().suspend()) { msg ->
effect {
raise<Int>(msg())
raise(msg())
}.runCont() shouldBe msg()
}
}
Expand Down Expand Up @@ -302,7 +302,7 @@ class EffectSpec :
"catch - error path and recover" {
checkAll(Arb.int().suspend(), Arb.string().suspend()) { int, fallback ->
effect<Int, String> {
raise<String>(int())
raise(int())
fail("It should never reach this point")
}.recover<Int, Nothing, String> { fallback() }
.runCont() shouldBe fallback()
Expand All @@ -312,7 +312,7 @@ class EffectSpec :
"catch - error path and re-raise" {
checkAll(Arb.int().suspend(), Arb.string().suspend()) { int, fallback ->
effect<Int, Unit> {
raise<String>(int())
raise(int())
fail("It should never reach this point")
}.recover { raise(fallback()) }
.runCont() shouldBe fallback()
Expand All @@ -323,7 +323,7 @@ class EffectSpec :
checkAll(Arb.int().suspend(), Arb.string().suspend()) { int, msg ->
shouldThrow<RuntimeException> {
effect<Int, String> {
raise<String>(int())
raise(int())
fail("It should never reach this point")
}.recover<Int, Nothing, String> { throw RuntimeException(msg()) }
.runCont()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ suspend fun <A> awaitExitCase(exit: CompletableDeferred<ExitCase>): A =
val error = "Error"
val exit = CompletableDeferred<ExitCase>()
effect<String, Int> {
parZip({ awaitExitCase<Int>(exit) }, { raise<Int>(error) }) { a, b -> a + b }
parZip({ awaitExitCase<Int>(exit) }, { raise(error) }) { a: Int, b: Int -> a + b }
}.fold({ it shouldBe error }, { fail("Int can never be the result") })
exit.await().shouldBeTypeOf<ExitCase>()
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ suspend fun main() {
val error = "Error"
val exit = CompletableDeferred<ExitCase>()
effect<String, Int> {
raceN({ awaitExitCase<Int>(exit) }) { raise<Int>(error) }
raceN({ awaitExitCase<Int>(exit) }) { raise(error) }
.merge() // Flatten Either<Int, Int> result from race into Int
}.fold({ msg -> msg shouldBe error }, { fail("Int can never be the result") })
// It's possible not all parallel task got launched, and in those cases awaitCancellation never ran
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ suspend fun main() {
resourceScope {
effect<String, Int> {
val reader = bufferedReader("build.gradle.kts")
raise<Int>(error)
raise(error)
reader.lineSequence().count()
}.fold({ it shouldBe error }, { fail("Int can never be the result") })
}
Expand Down

0 comments on commit 33bfa6b

Please sign in to comment.