Is there a current way to access a context receiver on a functional type or is that missing from the language right now?
Copy code
class C {
val a = 1
}
context(C)
fun foo() {
a // Works
this@C // Works
this@C.a // Works
}
val lambda: context(C) () -> Unit = {
a // Works
this@C // Does not work
this@C.a // Does not work
}
y
Youssef Shoaib [MOD]
02/15/2024, 2:33 PM
Copy code
context(A) fun <A> given(): A = this@A
given<C>() // Works
a
ansman
02/15/2024, 2:33 PM
Oof, nice workaround but feels somewhat broken that you need this?
y
Youssef Shoaib [MOD]
02/15/2024, 2:34 PM
It's actually what the new contexts KEEP is suggesting. It's good because it allows you to differentiate between different type args e.g.
List<String>
vs
List<Int>
❤️ 1
a
ansman
02/15/2024, 2:37 PM
Yeah I've seen that. Anyway, this helps me get unblocked, thanks!