-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow resuming Cont with exception
- Loading branch information
Showing
6 changed files
with
148 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import kotlin.jvm.JvmInline | ||
|
||
@JvmInline | ||
public value class Handle<Error, T> internal constructor(private val reader: Reader<suspend (Error) -> T>) { | ||
public constructor() : this(Reader()) | ||
|
||
@ResetDsl | ||
public suspend fun call(error: Error): T = reader.ask()(error) | ||
|
||
@ResetDsl | ||
public suspend fun <R> handle( | ||
handler: suspend (Error, Cont<T, R>) -> R, body: suspend () -> R | ||
): R { | ||
val prompt = Prompt<R>() | ||
return prompt.pushPrompt( | ||
extraContext = reader.context(DeepHandler(prompt, this, handler)::invoke), body = body | ||
) | ||
} | ||
|
||
private class DeepHandler<Error, T, R>( | ||
private val prompt: Prompt<R>, | ||
private val handle: Handle<Error, T>, | ||
private val handler: suspend (Error, Cont<T, R>) -> R | ||
) { | ||
suspend operator fun invoke(error: Error): T = prompt.takeSubCont { k -> | ||
handler(error) { | ||
k.pushSubContWith(it, isDelimiting = true, extraContext = handle.reader.context(::invoke)) | ||
} | ||
} | ||
} | ||
|
||
@ResetDsl | ||
public suspend fun <R> handleShallow( | ||
handler: suspend (Error, Cont<T, R>) -> R, body: suspend () -> R | ||
): R { | ||
val prompt = Prompt<R>() | ||
return prompt.pushPrompt(extraContext = reader.context(ShallowHandler(prompt, handler)::invoke), body = body) | ||
} | ||
|
||
private class ShallowHandler<Error, T, R>( | ||
private val prompt: Prompt<R>, | ||
private val handler: suspend (Error, Cont<T, R>) -> R | ||
) { | ||
suspend operator fun invoke(error: Error): T = prompt.takeSubCont { k -> | ||
handler(error) { result -> | ||
k.pushSubContWith(result) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@ResetDsl | ||
public suspend fun <Error, T, R> newHandle( | ||
handler: suspend (Error, Cont<T, R>) -> R, body: suspend Handle<Error, T>.() -> R | ||
): R = with(Handle<Error, T>()) { | ||
handle(handler) { body() } | ||
} | ||
|
||
@ResetDsl | ||
public suspend fun <Error, T, R> newHandleShallow( | ||
handler: suspend Handle<Error, T>.(Error, Cont<T, R>) -> R, body: suspend Handle<Error, T>.() -> R | ||
): R = with(Handle<Error, T>()) { | ||
handleShallow({ e, k -> handler(e, k) }) { body() } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import io.kotest.matchers.shouldBe | ||
import kotlinx.coroutines.test.runTest | ||
import kotlin.test.Test | ||
|
||
class HandleTest { | ||
@Test | ||
fun coroutineAndReader() = runTest { | ||
val printed = mutableListOf<Int>() | ||
runCC { | ||
runReader(10) { | ||
suspend fun Handle<Int, Unit>.handler(error: Int, cont: Cont<Unit, Unit>) { | ||
printed.add(error) | ||
handleShallow(::handler) { pushReader(ask() + 1) { cont(Unit) } } | ||
} | ||
newHandleShallow<Int, Unit, _>(Handle<Int, Unit>::handler) { | ||
call(ask()) | ||
call(ask()) | ||
pushReader(ask() + 10) { | ||
call(ask()) | ||
call(ask()) | ||
} | ||
} | ||
} | ||
} | ||
printed shouldBe listOf(10, 11, 21, 21) | ||
} | ||
|
||
@Test | ||
fun coroutineAndReaderWithNestedHandler() = runTest { | ||
val printed = mutableListOf<Int>() | ||
runCC { | ||
runReader(10) { | ||
suspend fun Handle<Int, Unit>.handler(error: Int, cont: Cont<Unit, Unit>) { | ||
handleShallow({ e, k -> | ||
printed.add(e) | ||
handler(e, k) | ||
}) { pushReader(ask() + 1) { cont(Unit) } } | ||
} | ||
newHandleShallow<Int, Unit, _>(Handle<Int, Unit>::handler) { | ||
call(ask()) | ||
call(ask()) | ||
pushReader(ask() + 10) { | ||
call(ask()) | ||
call(ask()) | ||
} | ||
} | ||
} | ||
} | ||
printed shouldBe listOf(11, 21, 21) | ||
} | ||
|
||
@Test | ||
fun readerSimulation() = runTest { | ||
runCC { | ||
newHandle<Unit, Int, _>({ _, cont -> cont(42) }) { | ||
call(Unit) shouldBe 42 | ||
handleShallow({ _, cont -> cont(43) }) { | ||
call(Unit) shouldBe 43 | ||
call(Unit) shouldBe 42 | ||
} | ||
call(Unit) shouldBe 42 | ||
handle({ _, cont -> cont(44) }) { | ||
call(Unit) shouldBe 44 | ||
call(Unit) shouldBe 44 | ||
} | ||
call(Unit) shouldBe 42 | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters