See the code below:
class A
class B
fun f(argF: context(A, B) () -> Unit) {
argF(A(), B())
}
fun main() {
// doesn't work
f {
println(this)
println(this@A)
println(this@B)
}
// doesn't work
f { a, b ->
println(a)
println(b)
}
// doesn't work
f(context(A, B) fun() {
println(this@A)
println(this@B)
})
// IntelliJ IDEA doesn't complain but it doesn't compile.
f(::helper)
// works
f { helper() }
}
context(A, B)
@Suppress("NOTHING_TO_INLINE")
private inline fun helper() {
println(this@A)
println(this@B)
}
I tried several approaches and it seems the only way I found that works now is to define a helper function with context receivers. Is there a way to refer to the receiver arguments inside the lambda?