https://kotlinlang.org logo
Title
l

LeoColman

02/20/2019, 2:39 PM
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

540grunkspin

02/20/2019, 2:41 PM
Didn’t even know that you could have lazily instantiated extension properties
s

streetsofboston

02/20/2019, 2:43 PM
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:
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

ilya.gorbunov

02/20/2019, 4:36 PM
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.