我想对var
属性进行延迟初始化。由于by lazy
仅限于val
属性,因此我必须编写如下内容:
private var currentContextProvider: ContextProvider? = null
get() {
if (field == null) {
field = DefaultContextProvider()
}
return field
}
现在我必须处理那些毫无意义的可空性调用:currentContextProvider?.getContext()
或currentContextProvider!!.getContext()
我做错了吗?
答案 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
}
}
}