Coroutines Concepts
Continuation Result
A continuation receives success or failure through a Result value.
continuation result
`Result` lets the continuation handle a produced value or an exception without changing the callback shape.
Continuation Result
ContinuationResult.kt
Replay: real traced execution (multi-file project)
import kotlin.coroutines.Continuation
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.coroutines.startCoroutine
suspend fun parsePositive(text: String): Int {
val number = text.toInt()
if (number <= 0) {
error("not positive")
}
return number
}
fun main() {
val text = "6"
var summary = "not resumed"
val work: suspend () -> Int = {
parsePositive(text)
}
work.startCoroutine(object : Continuation<Int> {
override val context = EmptyCoroutineContext
override fun resumeWith(result: Result<Int>) {
summary = result.fold(
onSuccess = { value -> "success=$value" },
onFailure = { error -> "failure=${error.message}" }
)
}
})
println(summary)
}
import kotlin.coroutines.Continuation
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.coroutines.startCoroutine
suspend fun parsePositive(text: String): Int {
val number = text.toInt()
if (number <= 0) {
error("not positive")
}
return number
}
fun main() {
val text = "0"
var summary = "not resumed"
val work: suspend () -> Int = {
parsePositive(text)
}
work.startCoroutine(object : Continuation<Int> {
override val context = EmptyCoroutineContext
override fun resumeWith(result: Result<Int>) {
summary = result.fold(
onSuccess = { value -> "success=$value" },
onFailure = { error -> "failure=${error.message}" }
)
}
})
println(summary)
}
import kotlin.coroutines.Continuation
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.coroutines.startCoroutine
suspend fun parsePositive(text: String): Int {
val number = text.toInt()
if (number <= 0) {
error("not positive")
}
return number
}
fun main() {
val text = "9"
var summary = "not resumed"
val work: suspend () -> Int = {
parsePositive(text)
}
work.startCoroutine(object : Continuation<Int> {
override val context = EmptyCoroutineContext
override fun resumeWith(result: Result<Int>) {
summary = result.fold(
onSuccess = { value -> "success=$value" },
onFailure = { error -> "failure=${error.message}" }
)
}
})
println(summary)
}
text ← 6, summary ← not resumed, work ← () -> kotlin.Int
13fun main() {14 val text→ 6 = "6" //@text="0", "9"15 var summary→ not resumed = "not resumed"1617 val work→ () -> kotlin.Int: suspend () -> Int = {18 parsePositive(text)19 }2021 work() -> kotlin.Int.startCoroutine(object : Continuation<Int> {22 override val context = EmptyCoroutineContext2324 override fun resumeWith(result: Result<Int>) {25 summary = result.fold(26 onSuccess = { value -> "success=$value" },27 onFailure = { error -> "failure=${error.message}" }28 )29 }30 })number ← 6
5suspend fun parsePositive(text6: String): Int {6 val number→ 6 = text6.toInt()7 if (number <= 0) {8 error("not positive")9 }10 return number611}override fun resumeWith(result: Result<Int>)
21work() -> kotlin.Int.startCoroutine(object : Continuation<Int> {22 override val context = EmptyCoroutineContext2324 override fun resumeWith(resultSuccess(6): Result<Int>) {25 summary = resultSuccess(6).fold(26 onSuccess = { value -> "success=$value" },27 onFailure = { error -> "failure=${error.message}" }28 )29 }30})println(summary)
32 println(summarysuccess=6)33}outputsuccess=6
text ← 0, summary ← not resumed, work ← () -> kotlin.Int
13fun main() {14 val text→ 0 = "0"15 var summary→ not resumed = "not resumed"1617 val work→ () -> kotlin.Int: suspend () -> Int = {18 parsePositive(text)19 }2021 work() -> kotlin.Int.startCoroutine(object : Continuation<Int> {22 override val context = EmptyCoroutineContext2324 override fun resumeWith(result: Result<Int>) {25 summary = result.fold(26 onSuccess = { value -> "success=$value" },27 onFailure = { error -> "failure=${error.message}" }28 )29 }30 })number ← 0
5suspend fun parsePositive(text0: String): Int {6 val number→ 0 = text0.toInt()7 if (number <= 0) {if (number <= 0)
6val number = text.toInt()7if (number0 <= 0) {8 error("not positive")9}override fun resumeWith(result: Result<Int>)
21work() -> kotlin.Int.startCoroutine(object : Continuation<Int> {22 override val context = EmptyCoroutineContext2324 override fun resumeWith(resultFailure(java.lang.IllegalStateException: not positive): Result<Int>) {25 summary = resultFailure(java.lang.IllegalStateException: not positive).fold(26 onSuccess = { value -> "success=$value" },27 onFailure = { error -> "failure=${error.message}" }28 )29 }30})println(summary)
32 println(summaryfailure=not positive)33}outputfailure=not positive
text ← 9, summary ← not resumed, work ← () -> kotlin.Int
13fun main() {14 val text→ 9 = "9"15 var summary→ not resumed = "not resumed"1617 val work→ () -> kotlin.Int: suspend () -> Int = {18 parsePositive(text)19 }2021 work() -> kotlin.Int.startCoroutine(object : Continuation<Int> {22 override val context = EmptyCoroutineContext2324 override fun resumeWith(result: Result<Int>) {25 summary = result.fold(26 onSuccess = { value -> "success=$value" },27 onFailure = { error -> "failure=${error.message}" }28 )29 }30 })number ← 9
5suspend fun parsePositive(text9: String): Int {6 val number→ 9 = text9.toInt()7 if (number <= 0) {8 error("not positive")9 }10 return number911}override fun resumeWith(result: Result<Int>)
21work() -> kotlin.Int.startCoroutine(object : Continuation<Int> {22 override val context = EmptyCoroutineContext2324 override fun resumeWith(resultSuccess(9): Result<Int>) {25 summary = resultSuccess(9).fold(26 onSuccess = { value -> "success=$value" },27 onFailure = { error -> "failure=${error.message}" }28 )29 }30})println(summary)
32 println(summarysuccess=9)33}outputsuccess=9