带有重置的惰性变量 [英] Lazy variable with reset

查看:90
本文介绍了带有重置的惰性变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个不为null的特定类型的变量,例如说Foo.

I want to create a variable of a certain type that is not null, say Foo for example.

然后,我希望所有对变量的访问都返回Foo,就像一个懒惰的委托一样,但是,我也希望能够重置它.

I then want all access to the variable to return Foo, just like a lazy delegate, however, I also want to be able to reset it.

类似的东西:

var foo : String by Foo(init: {"bar"})

print(foo) // prints "bar"
foo = null // or foo.reset()
print(foo) // prints "bar"

我要解决的问题: 我有一个适配器索引,当适配器内容更改时需要重新创建.因此,在更改时,我想清除索引,而下次有人尝试访问它时,我想重新创建它.

The problem I am trying to solve: I have an index for an adapter that I need to recreate when the adapter content changes. So on change I want to clear the index, and the next time someone tries to accessing it, I want to recreate it.

推荐答案

如果目标是延迟初始化的var property可以重置为初始状态,则可以改用Kotlin的

If the goal is to have a lazy initialized var property which can be reset to it's initial state you can adapt Kotlin's SynchronizedLazyImpl to allow the invalidate feature:

private object UNINITIALIZED_VALUE
class InvalidatableLazyImpl<T>(private val initializer: () -> T, lock: Any? = null) : Lazy<T>, Serializable {
    @Volatile private var _value: Any? = UNINITIALIZED_VALUE
    private val lock = lock ?: this
    fun invalidate(){
        _value = UNINITIALIZED_VALUE
    }

    override val value: T
        get() {
            val _v1 = _value
            if (_v1 !== UNINITIALIZED_VALUE) {
                return _v1 as T
            }

            return synchronized(lock) {
                val _v2 = _value
                if (_v2 !== UNINITIALIZED_VALUE) {
                    _v2 as T
                }
                else {
                    val typedValue = initializer()
                    _value = typedValue
                    typedValue
                }
            }
        }


    override fun isInitialized(): Boolean = _value !== UNINITIALIZED_VALUE

    override fun toString(): String = if (isInitialized()) value.toString() else "Lazy value not initialized yet."

    operator fun setValue(any: Any, property: KProperty<*>, t: T) {
        _value = t
    }
}

然后可以按以下方式使用:

Which could then be used as follows:

private val fooDelegate = InvalidatableLazyImpl({"bar"})
var foo:String by fooDelegate

println(foo); // -> "bar"
foo = "updated"
println(foo); // -> "updated"
fooDelegate.invalidate()
println(foo); // -> "bar"

很明显,可以修改委托实现,以允许null值用作重置,但是这可能会使代码难以推理,即:

One could obviously modify the delegate implementation to allow for null value to act as a reset however it could make the code harder to reason about i.e:

println(obj.foo); //-> prints "bar
obj.foo = null //reset the value, implicitely
println(obj.foo); //-> prints "bar", but hey didn't I just said `null`

这篇关于带有重置的惰性变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆