I've got a possibly outdated coroutine question. I...
# coroutines
e
I've got a possibly outdated coroutine question. I think the past the continuations worked something like this:
Copy code
suspend fun justForDemo(duration: Duration): Int {
  delay(duration)
  return (0..200).random()
}
suspend fun doSomething(value: Int) {
  println("Before sleep")
  val random = justForDemo(value.seconds)
  println("After sleep: ${random}")
}
turned into ig like:
Copy code
fun doSomething(continuation: Continuation, value: Int) {
  //point continuation to this function
  val result: Any
  switch (continuation.branch) {
    case 0:
     println("Before sleep")
     continuation.branch= 1
     val result = justForDemo(continuation, value.seconds)
     if (result === COROUTINE_SUSPENDED) return
     continuation.result = Result.success(result)
    case 1:
     if (continuation.result.isFailure)
       throw continuation.result.error
     val random = continuation.result.value
     println("After Sleep: ${random}")
  }
}
I haven't checked recently and might not have remembered correctly. My first question is, why put the COROUTINE_SUSPENDED flag through the return? The result is that the function cannot return primitives without boxing at which point you may as well have put both the result or the suspended flag directly into the continuation itself. Then it would look more like: justForDemo(continuation, value.seconds) if (continuation.suspended) return My next question is, is there still a single continuation instance per job (i think roughly?) Finally, there isn't anything wrong with resuming a continuation before returning COROUTINE_SUSPENDED right? In my case I want to sometimes block the thread that called the suspendign function before it gets a chance to return however a different thread has a chance to resume the continuation before the blocking completes. I imagine the answer is "yes it's fine" otherwise it wouldn't be very thread safe.