Skip to content

Remove InternalForInheritanceCoroutinesApi from Deferred<T> #4408

@Flavien

Description

@Flavien

What do we have now?

I want to return a CompletableDeferred<T> from a public API. I am returning it as a Deferred<T> as I don't want the consumer of the API to be able to complete the deferred, as this would mess with the internals class returning the deferred. If I just return the CompletableDeferred<T>, it can still be tempting for a consumer to upcast Deferred<T> to CompletableDeferred<T> and complete it.

So I do the following instead:

private class ReadOnlyDeferred<T>(source: Deferred<T>) : Deferred<T> by source

fun <T> execute(): Deferred<T> {
  // some code
  return ReadOnlyDeferred(result)
}

This would be a clean solution, but unfortunately I get this warning:

This class or interface requires opt-in to be implemented: This is a kotlinx. coroutines API that is not intended to be inherited from, as the library may handle predefined instances of this in a special manner. This will be an error in a future release. If you need to inherit from this, please describe your use case in https:// github. com/ Kotlin/ kotlinx. coroutines/ issues, so that we can provide a stable API for inheritance.

What should be instead?

There should be a way to achieve the above scenario without relying on internal APIs. Either provide a function allowing to return a "locked" version of the deferred:

fun <T> Deferred<T>.lock(): Deferred<T>

This is similar to CompletableFuture<T>.minimalCompletionStage().

Or remove InternalForInheritanceCoroutinesApi from Deferred<T>.

Why?

This would allow basic encapsulation for Deferred.

Activity

added 2 commits that reference this issue on Apr 9, 2025
c3bcc24
28466cb
dkhalanskyjb

dkhalanskyjb commented on Apr 9, 2025

@dkhalanskyjb
Collaborator

Thanks! The use case is clear, and we already have things like https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/as-state-flow.html, so adding this is straightforward: #4410

Flavien

Flavien commented on Apr 9, 2025

@Flavien
Author

Wow, that was fast, thanks @dkhalanskyjb .

added a commit that references this issue on Apr 10, 2025
4ba95e0
added a commit that references this issue on Apr 29, 2025
16e5f6f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Remove InternalForInheritanceCoroutinesApi from Deferred<T> · Issue #4408 · Kotlin/kotlinx.coroutines