Skip to content

Enhance CoroutineDispatcher.invoke with contract - #4554

Open
LouisCAD wants to merge 1 commit into
Kotlin:developfrom
LouisCAD:patch-1
Open

Enhance CoroutineDispatcher.invoke with contract#4554
LouisCAD wants to merge 1 commit into
Kotlin:developfrom
LouisCAD:patch-1

Conversation

@LouisCAD

Copy link
Copy Markdown
Contributor

Add contract to CoroutineDispatcher.invoke operator, to allow initializing read-only properties declared before, from inside the passed block.

https://youtrack.jetbrains.com/issue/KT-32313/Support-contracts-for-operator-functions

This commit also improve the KDoc to show a usage example, and explain why it's less error-prone than withContext.

Add contract to CoroutineDispatcher.invoke operator,
to allow initializing read-only properties declared before,
from inside the passed block.

https://youtrack.jetbrains.com/issue/KT-32313/Support-contracts-for-operator-functions

This commit also improve the KDoc to show a usage example,
and explain why it's less error-prone than withContext.
@qwwdfsad
qwwdfsad self-requested a review November 7, 2025 18:16

@qwwdfsad qwwdfsad left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Could you please also add a test for that? (i.e. like RunBlocking.testContract)

*
* Example usage:
* ```
* Dispatchers.IO { someFile.exists() } // Checking the file system is blocking I/O.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Dispatchers.IO { someFile.exists() } // Checking the file system is blocking I/O.
* Dispatchers.IO {
* someFile.exists() // Checking the file system is blocking I/O
* }

@dkhalanskyjb

Copy link
Copy Markdown
Collaborator

Let's reach a conclusion on #4199 first before going through with this, so that we don't risk doing extra work that will ultimately not be used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants