I don't understand: can't get context parameters f...
# general-advice
s
I don't understand: can't get context parameters from lambda function parameter. Am I doing something wrong here? Context parameters cannot be named here, so I really don't understand how this is supposed to work.
Copy code
fun 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 🙏
r
> // error: CoroutineScope is not applied ?? For action1 and action2, you have to give it a
CoroutineScope
.
Copy code
// 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 ??
}
action3
gets its scope from `runBlocking`:
Copy code
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
    }
}
action2
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:
Copy code
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)
    }
}
s
Hey @Ryan Payne, thanks for getting back to me! I think I gave misleading examples. I'm not actually interested in the coroutine aspect, I'm specifically trying to understand context parameters. In your example, action1, action2 and action3 are "solved" by using a reference that isn't provided by
context()
. Here's a minimal example that compiles. I'm wondering: how can I access all the context parameters inside
tryThis { ... }
?
Copy code
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())
}
r
You can't access them. They don't get passed into the context of the
block
lambda.
Update: Answered farther down: https://kotlinlang.slack.com/archives/CR7H6LTD5/p1785463930831609?thread_ts=1785426554.397149&cid=CR7H6LTD5
What's the use case here? Why don't you pass them as parameters?
Copy code
fun 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())
}
s
I don't understand then. Why would the language be able to have context parameters on function lambda parameters if they can't be used? This seems like a language design issue.
My use case was with context receivers, the predecessor of context parameters where everything was bundled in
this
, 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).
r
I don't have the answer to that question. However, I wonder if "Named context parameters in function types" is coming at some point in the future. Update: Answered: https://kotlinlang.slack.com/archives/CR7H6LTD5/p1785463930831609?thread_ts=1785426554.397149&cid=CR7H6LTD5
⭐ 1
l
I believe you are looking for contextOf.
☝️ 1
today i learned 1
⭐ 1
s
Yes. thank you so much 🙏 I thought I saw something like thatsomewhere! but forget where