Kotlin var lazy init

时间:2017-12-22 21:59:58

标签: delegates kotlin

我想对var属性进行延迟初始化。由于by lazy仅限于val属性,因此我必须编写如下内容:

    private var currentContextProvider: ContextProvider? = null
        get() {
            if (field == null) {
                field = DefaultContextProvider()
            }
            return field
        }

现在我必须处理那些毫无意义的可空性调用:currentContextProvider?.getContext()currentContextProvider!!.getContext()

我做错了吗?

1 个答案:

答案 0 :(得分:4)

您可以决定使用某个默认值对其进行初始化,而不是使其可以为空,初始访问权限将替换为延迟计算的值:

private val noInit = "noinit"
var currentContextProvider: String = noInit
        get() = if (field == noInit) {
            synchronized(this) {
                return if (field == noInit) "lazyinit" else field
            }
        } else field

(我已将ContextProvider替换为String

自定义代表

以下实现了custom delegate重用前一个解决方案。通过定义lazy()

,它可以像var currentContextProvider: ContextProvider by LazyMutable(){DefaultContextProvider()}一样使用
class LazyMutable<T>(val initializer: () -> T) : ReadWriteProperty<Any?, T> {
    private object UNINITIALIZED_VALUE
    private var prop: Any? = UNINITIALIZED_VALUE

    @Suppress("UNCHECKED_CAST")
    override fun getValue(thisRef: Any?, property: KProperty<*>): T {
        return if (prop == UNINITIALIZED_VALUE) {
            synchronized(this) {
               return if (prop == UNINITIALIZED_VALUE) initializer().also { prop = it } else prop as T
            }
        } else prop as T
    }

    override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
        synchronized(this) {
            prop = value
        }
    }
}