https://kotlinlang.org logo
#language-proposals
Title
# language-proposals
e

Emil Kantis

07/04/2022, 6:35 PM
Will it be possible to pass function references to functions using context receivers in the future? E.g.
Copy code
context(MyContext)
fun sayHello(name: String) {
  println("Hello $name")
}

fun main() {
  with(context) {
    listOf("a", "b").forEach(::sayHello)
  }
}
Right now it gives an error because sayHello is a
KFunction2<MyContext, String,Unit>
when a
(TypeVariable(T)) -> Unit
was expected.
3
e

elizarov

10/20/2022, 7:51 AM
The interaction of context receivers with callable references is still to be designed. Here you are looking for some sort of “context-bound callable references” similar to callable references to members. It creates lots of design question we don’t have any answer for. I’ve opened an issue here https://youtrack.jetbrains.com/issue/KT-54594/Context-bound-callable-references-to-contextual-functions