Is there any use of a mutable extension property i...
# getting-started
d
Is there any use of a mutable extension property if that property doesn't have a backing field? I.e. is there any way how I can set the value of the property and then retrieve it later via
get()
?
s
Well, if you don't have a backing field, you need somewhere else to store the value. A
MutableMap
would work.
Copy code
var Foo.bar: String
  set(value) { bars[this] = value }
  get() = bars[this]!!

private val bars = mutableMapOf<Foo, String>()
d
I see. I've also found a more elaborate answer: https://stackoverflow.com/questions/70630421/kotlin-implement-extension-var-property. tl;dr: Use
WeakHashMap