Skip to content

Coroutine equivalent of Thread.currentThread() requires a !! cast #2159

Closed
@qwwdfsad

Description

@qwwdfsad

The closest equivalent to the JVM's Thread.currentThread() for coroutines is

coroutineContext[Job]!!

This !! cast always succeeds, but it draws the developer's attention and brings in uncertainty about the correctness of the code. Here's an example where this concern was raised on Stack Overflow.

Kotlin could provide a function that hides this check, for example:

suspend fun currentJob(): Job = coroutineContext[Job]!!

Originally reported here

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