I'm currently learning Rust and especially traits. I've read KEEP-87 as well as the contextual receivers one previously, and I've seen mentioned multiple times that they will allow to implement an interface for a class we don't control. I understand how KEEP-87 and Rust's traits manage this, but how do context receivers enable this?
r
raulraja
11/04/2021, 10:57 AM
Hi @CLOVIS, Context receivers emulate injection by exposing the extension syntax in the function body but you still need to call
with
or
run
at some point to materialize their scope. They replace implicit summoning by making the contextual function only resolvable when they are scoped in the intersection of all declared context types.
Copy code
context(A, B)
fun foo(): Unit {}
a.run { b.run { foo() }} //ok
foo() //not ok, kotlin won't summon providers for A and B, wants you to be explicit