Rafael Costa
08/13/2025, 9:52 AMfun bar(
lambda: context(String) () -> Unit
) {
with("") {
lambda()
}
}
fun foo() {
bar {
this // error
}
}
Am I missing something? Is this not supposed to give me a string?
I am not able to get the context parameter of the lambda in any way. If I try bar { str ->
then "str" here is also an error 🤔Joffrey
08/13/2025, 9:56 AMthis
is no longer the correct syntax to access context receivers, you should now use implicit<...>()
. See the KEEP: https://github.com/Kotlin/KEEP/blob/context-parameters/proposals/context-parameters.md#standard-library-supportRafael Costa
08/13/2025, 9:59 AMRafael Costa
08/13/2025, 10:02 AMinterface Logger {
fun logme(msg: String)
}
fun bar(
lambda: context(Logger) () -> Unit,
) {
val logger = object : Logger {
override fun logme(msg: String) = println(msg)
}
with(logger) {
lambda()
}
}
fun foo() {
bar {
logme("asd")
}
}
Rafael Costa
08/13/2025, 10:02 AMJoffrey
08/13/2025, 10:50 AMimplicit
function wasn't implemented 🤔Joffrey
08/13/2025, 10:51 AMlogme
is defined with Logger
as receiver. If you define another logme
function with context parameter, it would work:
context(logger: Logger)
fun logme(msg: String) = logger.logme(msg)
Joffrey
08/13/2025, 10:52 AMYoussef Shoaib [MOD]
08/13/2025, 10:57 AMcontextOf
? I can see it in Kotlin 2.2 projectsRafael Costa
08/13/2025, 11:11 AMRafael Costa
08/13/2025, 11:13 AMRafael Costa
08/13/2025, 11:18 AMimplicit()
.
For named params case, I think it's fine to just use the name 🙂