https://kotlinlang.org logo
#announcements
Title
# announcements
s

steenooo

12/01/2019, 7:56 PM
How do property delegation work? Extension properties can't have a value. but I can delegate them with lazy? Where is the lazy value stored?
d

Dico

12/01/2019, 11:52 PM
An extension property delegate's getter delegates calls to a global delegate object. In the case of lazy, there can be no references to the receiver. The getValue operator does get a reference to the receiver in the first parameter. It could conceivably use some kind of map to store an attribute separately for each instance. Delegates allow you to encapsulate this kind of logic.
k

karelpeeters

12/02/2019, 9:10 AM
So in other words,
val Foo.x by lazy { ... }
means there is only a singe
Lazy
instance that is "shared" by all
Foo
instances, so there's no real point if even making it an extension property.
💯 2
26 Views