Is there any alternative for `lazy` that includes ...
# announcements
l
Is there any alternative for
lazy
that includes the receiver? So that I can do
val Foo.bar by lazy { this.baz }
where
this
is
Foo
u
Didn’t even know that you could have lazily instantiated extension properties
s
You can even have delegators on plain `val`s in a function body.
To answer your question: No, there isn’t. You can write your own Delegator:
Copy code
class MyLazyDelegate<T>  {
    // For class properties
    operator fun getValue(receiver: Any, property: KProperty<*>): T {
        TODO()
    }
    
    // For stack-frame/heap vals.
    operator fun getValue(receiver: Nothing?, property: KProperty<*>): T {
        TODO()
    }
}
i
Note that it's a single delegate instance per extension property, so that if you want to store some value for each
Foo
object being extended, you're gonna need some external storage for that.