commonMain.fcontrol.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kontinuity Show documentation
Show all versions of kontinuity Show documentation
Provides fully-fledged multishot delimitied continuations in Kotlin with Coroutines
The newest version!
import kotlin.jvm.JvmInline
@JvmInline
public value class Fcontrol private constructor(private val reader: Reader T>) {
public constructor() : this(Reader())
@ResetDsl
public suspend fun fcontrol(error: Error): T = reader.ask()(error)
@ResetDsl
public suspend fun resetFcontrol(
handler: suspend (Error, SubCont) -> R, body: suspend () -> R
): R {
val prompt = Prompt()
return reader.pushReader({ prompt.takeSubCont(false) { k -> handler(it, k) } }) {
prompt.pushPrompt(body)
}
}
@ResetDsl
public suspend fun resetFcontrol0(
handler: suspend (Error, SubCont) -> R, body: suspend () -> R
): R {
val prompt = Prompt()
return reader.pushReader({
prompt.takeSubCont { k ->
reader.deleteBinding()
handler(it, k)
}
}) {
prompt.pushPrompt(body)
}
}
}
@ResetDsl
public suspend fun newResetFcontrol(
handler: suspend Fcontrol.(Error, SubCont) -> R, body: suspend Fcontrol.() -> R
): R = with(Fcontrol()) {
resetFcontrol({ e, k -> handler(e, k) }) { body() }
}