class Foo { fun get(): Something }
operator fun Bar.getValue(o: Any?, property: KProperty<*>): Something = Foo().get()
val prop by Bar()
Will the Foo instance get GCed?
r
Raymond
12/08/2021, 6:34 PM
Looks like a yes to me, as the instance of Foo lives inside the scope of the getValue function.
Raymond
12/08/2021, 6:35 PM
the thing changes if “Something” keeps a reference of the Foo class
d
dave08
12/08/2021, 6:50 PM
Lazy seems to do something similar, the difference is that the reciever is Lazy<T>, and that has to not have been GCed... Is that some special case?
r
Raymond
12/08/2021, 7:16 PM
the Lazy implementation keeps an internal reference to its output. You can check that behavior on the SynchronizedLazyImpl class
d
dave08
12/08/2021, 7:29 PM
I also have a reference to the Something in Foo, I just don't know if Foo gets created on each access or gets stored once and just the get() is called on access... Actually, I'm pretty sure it gets recreated, I'm just not to sure how to avoid that and still keep the reciever not to be Foo