-
Notifications
You must be signed in to change notification settings - Fork 4
Implement parameterize as multi-shot coroutine #34
Description
This is not a planned feature, at least not yet, but this issue tracks how parameterize could work if it were implemented as a suspend block that resumes from each parameter instead of re-running the whole block
It could be implemented as a suspend point when declaring a parameter:
suspend operator fun Parameter.provideDelegate()A parameter will be constructed only once (instead of every iteration). After that, there will be a suspend point when provideDelegate is called on it, and having the listed/computed arguments locked in. From there, parameterize's suspend block will resume from there once for each of the arguments as a multi-shot coroutine.
Benefits
- performance: no need to re-run start of block
- changes how code before a parameter behaves, since now only
- performance: no need to validate that the block was deterministic
- no need to throw
ParameterizeContinueup the call stack for internal control flow - no requirement for the block to be deterministic
parameterOfis no longer a performance concern- lazy
parameterizeis no longer needed
- lazy
Problems/Blockers
- Marking
provideDelegateas suspend is not supported as of Kotlin 2.0- Though that was a restriction with the K1 compiler architecture <----------------------------- LINK
- And has been acknowledged as possible to support in K2 <-------------------------------------- LINK
- Use inside other suspend blocks may (or may not) be an issue
- E.g. parameterizing Kotest suites with
context("group") { parameterize { // ... test("test") { // suspends with `context`'s coroutine scope, through parameterize's } } }
- E.g. parameterizing Kotest suites with
Implementation
Multi-shot coroutines should be possible as long as a coroutine continuation can be cloned, that way it can be resumed more than once.
For each platform:
- On jvm, Roman Elizarov has a Gist of how this can be done here
- For js, creating a shallow copy is also a common enough concept
- And native should be similarly straightforward