Are Context Parameters going to solve the "I want to create an extension that requires multiple receivers?" Example in 🧵
Daniel Pitts
12/14/2024, 2:45 AM
Copy code
open class FooAccess {
fun createFoo() = Foo()
}
class Foo {
companion object
}
class Bar : FooAccess() {
val foo by Foo
}
// In the context of FooAccess
operator fun Foo.Companion.getValue(thisRef: Any?, property: KProperty<*>): Foo = createFoo()
Daniel Pitts
12/14/2024, 2:46 AM
In this example, one could put the getValue method within FooAccess, but there are situations where that doesn't make sense to do.
Daniel Pitts
12/14/2024, 2:49 AM
In my case, its more about correct package dependency graph.
Foo
is a low-level property,
FooAccess
is a bridge between low and high level,
Bar
is a high-level class, and
getValue
has logic that needs to be aware of high-level classes, so doesn't belong in the bridge package.
y
Youssef Shoaib [MOD]
12/14/2024, 2:54 AM
Yes that should work. In addition, you could already do some of this by specifying
thisRef
to be a
FooAccess
, but that would limit its use to only be inside that class
d
Daniel Pitts
12/14/2024, 2:56 AM
Ah, that makes sense. That's probably would want anyway for this case, though getValue is just one example, not all of my use-cases are property delegation.