Stephcraft
07/30/2026, 3:49 PMfun main() {
action1 {
isDirectory // from File
launch {} // error: CoroutineScope is not applied ??
}
action2 {
isDirectory // error: File is not applied ??
launch {} // from CoroutineScope
}
action3 {
isDirectory // error: File is not applied ??
launch {} // error: CoroutineScope is not applied ??
}
}
fun action1(block: context(CoroutineScope) File.() -> Unit) {
runBlocking {
val file = File("README.md")
block(this, file)
}
}
fun action2(block: context(File) CoroutineScope.() -> Unit) {
runBlocking {
val file = File("README.md")
block(file, this)
}
}
fun action3(block: context(File, CoroutineScope) () -> Unit) {
runBlocking {
val file = File("README.md")
block(file, this)
}
}
fun action1(block: context(name: CoroutineScope) File.() -> Unit)
^ Named context parameters in function types are unsupported. Use syntax 'context(Type)' instead.
Thanks 🙏Ryan Payne
07/30/2026, 6:06 PMCoroutineScope.
// Example scope - Use the correct one for your use case.
val scope = CoroutineScope(EmptyCoroutineContext)
action1 {
isDirectory // from File
scope.launch {} // error: CoroutineScope is not applied ??
}
action3 {
isDirectory // error: File is not applied ??
scope.launch {} // error: CoroutineScope is not applied ??
}Ryan Payne
07/30/2026, 6:13 PMaction3 gets its scope from `runBlocking`:
action2 {
this.launch {} // <<< 'this' is runBlocking's scope
}
fun action2(block: context(File) CoroutineScope.() -> Unit) {
runBlocking {
val file = File("README.md")
block(file, this) <<< this == scope
}
}Ryan Payne
07/30/2026, 6:17 PMaction2 and action3 won't compile the way you're passing File. Named context parameters aren't supported.
Instead you can pass File as a parameter of the lambda:
fun main() {
val scope = CoroutineScope(EmptyCoroutineContext)
action2 { file ->
file.isDirectory
launch {}
}
action3 { file ->
file.isDirectory
scope.launch {}
}
}
fun action2(block: CoroutineScope.(File) -> Unit) {
runBlocking {
val file = File("README.md")
block(this, file)
}
}
fun action3(block: context(CoroutineScope) (File) -> Unit) {
runBlocking {
val file = File("README.md")
block(this, file)
}
}Stephcraft
07/30/2026, 6:41 PMcontext(). Here's a minimal example that compiles. I'm wondering: how can I access all the context parameters inside tryThis { ... }?
import java.io.File
import java.time.LocalDate
import java.util.UUID
fun main() {
tryThis {
// how do I access any of File, UUID, LocalDate here?
}
}
fun tryThis(block: context(File, UUID, LocalDate) () -> Unit) {
block(File("README.md"), UUID.randomUUID(), LocalDate.now())
}Ryan Payne
07/30/2026, 9:31 PMblock lambda.Ryan Payne
07/30/2026, 9:38 PMRyan Payne
07/30/2026, 9:38 PMfun main() {
tryThis { file, uuid, localDate ->
println(file)
println(uuid)
println(localDate)
}
}
fun tryThis(block: (File, UUID, LocalDate) -> Unit) {
block(File("README.md"), UUID.randomUUID(), LocalDate.now())
}Stephcraft
07/30/2026, 10:39 PMStephcraft
07/30/2026, 10:41 PMthis , and you could use this@File , this@UUID , this@LocalDate to be more precise. I'm converting my codebase, because context receivers have be removed from the language (and replaced by context parameters).Ryan Payne
07/30/2026, 10:43 PMLetter
07/31/2026, 2:12 AMStephcraft
07/31/2026, 5:21 PM