Kotlin / kotlinx.coroutines

Library support for Kotlin coroutines
Apache License 2.0
12.77k stars 1.82k forks source link

Remove @ExperimentalCoroutinesApi from CancellableContinuation.resume(...) #4088

Closed swankjesse closed 4 days ago

swankjesse commented 1 month ago

I’d like to use this overload of CancellableContinuation.resume() in OkHttp’s coroutines extension.

    @ExperimentalCoroutinesApi // since 1.2.0
    public fun resume(value: T, onCancellation: ((cause: Throwable) -> Unit)?)

Unfortunately, this functino is experimental. There’s no stable alternative that combines continuations with safe resource management. I’m going to annotate our own API with @ExperimentalCoroutinesApi to pass the responsibility to our callers.

I’d like for you to consider removing the experimental annotation from this function. It hasn’t changed since 2020. It solves an important very nicely!

qwwdfsad commented 1 month ago

We've been sitting on that for a while for quite a specific reason: its signature should've been (value: T, cause: Throwable) -> Unit, otherwise, when serving its main purpose, it is a capturing lambda that always allocates, even though there are no real reasons to. Changing that, there is a variance conflict -- continuations are contravariant (in T) and functional types are covariant (out T), which forces us to fallback to @UnsafeVariance and ensure that it's sound.

There was an extended attempt (https://github.com/Kotlin/kotlinx.coroutines/pull/3093) to change that and also receive a CoroutineContext as the third parameter, though this part remains questionable, and we never figured it out.

Taking into account it's experimental for so long if we are going to change the signature and deprecate it, this version is going to stay basically forever.

If you have any opinions on the current or potential API shape -- don't hesitate to tell us!

swankjesse commented 1 month ago

Thanks for the explanation. I hadn’t expected covariance!