Suspend Keyword

Suspend keyword is part of Kotlin StdLib,
but Is there any use cases besides coroutines ?

A) Yes. Can you provide some explanation, doc or example about it.
I dig it and all I can find is some kotlin.coroutines.* Continuation/CoroutineContext/startCoroutinemetc.) stuff all related to Coroutines.
I’m out of idea and I want to understand

B) No. Then shouldn’t we move Suspend.K to the Kotlinx.coroutines library ? And also all kotlin.coroutines* ?

Thanks,
Laurent

Hi there,
My guess is that Jetbrains wanted coroutines to be decoupled from executors implementations, like Rust did for its futures where you have a variety of implementations (tokio, async-st). Moreover it allow library developers to expose suspending APIs without relying on a specific version of kotlinx.coroutines, which is nice.

In addition playing with barebone coroutines helps you understand how it truly works and sometimes, for very basic stuff, you could use coroutines without kotlinx.coroutine (tho I wont try to do it outside of personal projects)

2 Likes

The sequences builder uses suspension.
Here’s the builder function in the stdlib: sequence - Kotlin Programming Language

The suspend modifier is required only for coroutine, it enables CPS.

The Sequence builder is a particular coroutine, it is a RestrictsSuspension.

Regarding your point take a look here: https://youtrack.jetbrains.com/issue/KT-22766

2 Likes