bbaldino
03/19/2019, 8:25 PMbbaldino
03/19/2019, 8:26 PMstreetsofboston
03/19/2019, 8:27 PMvar
instead?bbaldino
03/19/2019, 8:28 PMstreetsofboston
03/19/2019, 8:28 PMbbaldino
03/19/2019, 8:29 PMbbaldino
03/19/2019, 8:29 PMstreetsofboston
03/19/2019, 8:32 PMCached<T : Any>
that caches values of type T
, creates/parses it when it is not cached and allows the value to be cleared (cache-clear)?bbaldino
03/19/2019, 8:32 PMbbaldino
03/19/2019, 8:32 PMbbaldino
03/19/2019, 8:33 PMlazy
implementation and adding an invalidate
streetsofboston
03/19/2019, 8:33 PMstreetsofboston
03/19/2019, 8:34 PMbbaldino
03/19/2019, 8:34 PMbbaldino
03/19/2019, 8:36 PMbbaldino
03/19/2019, 8:36 PMbbaldino
03/19/2019, 8:36 PMbbaldino
03/19/2019, 8:36 PMinvalidate
streetsofboston
03/19/2019, 8:39 PMclass CacheDelegate<T : Any>(private var initialValue: T? = null, private val factory: () -> T) {
operator fun getValue(owner: Any, property: KProperty<*>) : T {
if (initialValue == null) {
initialValue = factory()
}
return initialValue!!
}
operator fun setValue(owner: Any, property: KProperty<*>, value : T) {
initialValue = value
}
fun invalidate() {
initialValue = null
}
}
class TestClass(
testValueDel : CacheDelegate<Int>
) {
var testValue: Int by testValueDel
}
fun main() {
val delegate = CacheDelegate {
4
}
val testClass = TestClass(delegate)
val x = testClass.testValue
testClass.testValue = 234
...
delegate.invalidate()
}
bbaldino
03/19/2019, 8:41 PMvar dirtyFlag: Boolean
in TestClass
and pass that as a property reference to CacheDelegate
, so that way i can avoid a separate member for the delegatebbaldino
03/19/2019, 8:41 PMstreetsofboston
03/19/2019, 8:42 PMTestClass
doesn’t have to worry about invalidating caches, nor the code that uses the TestClass
.bbaldino
03/19/2019, 8:42 PMbbaldino
03/19/2019, 8:42 PMstreetsofboston
03/19/2019, 8:42 PMCacheDelegate
class here is lazy… 🙂
Ah.. yep, then just a simple boolean suffices; maybe add get()
and set(value)
for those properties …bbaldino
03/19/2019, 8:44 PMlazy
bbaldino
03/19/2019, 8:44 PMbbaldino
03/19/2019, 8:48 PMinvalidate
method