在 Kotlin 中访问属性委托 [英] Access property delegate in Kotlin
问题描述
Kotlin 具有委托属性,这是一个非常好的特性.但有时 get()
和 set()
方法是不够的.假设我想懒惰地创建一个 Closeable
对象并稍后关闭它.以下是如何实现此类委托属性的示例:
Kotlin has delegated properties which is a very nice feature. But sometimes get()
and set()
methods are not enough. Let's say I want to create a Closeable
object lazily and to close it later. Here's an example of how such delegate property could be implemented:
fun <T : Closeable> closeableLazy(initializer: () -> T) =
CloseableLazyVal(initializer)
class CloseableLazyVal<T : Closeable>(
private val initializer: () -> T
) : ReadOnlyProperty<Any?, T> {
private var value: T? = null
override fun get(thisRef: Any?, desc: PropertyMetadata): T {
if (value == null) {
value = initializer()
}
return value
}
fun close() {
value?.close()
}
}
这就是我想使用它的方式:
And that's how I would like to use it:
private val stream by closeableLazy { FileOutputStream("/path/to/file") }
fun writeBytes(bytes: ByteArray) {
stream.write(bytes)
}
override fun close() {
stream::delegate.close() // This line will not compile
}
不幸的是,这种方法不起作用,因为 Kotlin 似乎不允许直接访问属性委托.有什么办法可以做我想做的事吗?或者是否有任何计划将此类功能添加到 Kotlin,因为它会是一个非常简洁的功能.
Unfortunately, this approach doesn't work because it seems that Kotlin doesn't allow to access property delegates directly. Is there any way to do what I want? Or are there any plans to add such functionality to Kotlin because it would be such a neat feature.
推荐答案
好的,所以我想出了以下解决方案:
Ok, so I came up with the following solution:
fun <T : Closeable> closeableLazy(initializer: () -> T) =
CloseableLazyVal(initializer)
class CloseableLazyVal<T : Closeable>(
private val initializer: () -> T
) : ReadOnlyProperty<CloseableDelegateHost, T> {
private var value: T? = null
override fun get(thisRef: CloseableDelegateHost, desc: PropertyMetadata): T {
if (value == null) {
value = initializer()
thisRef.registerCloseable(value!!)
}
return value!!
}
}
interface CloseableDelegateHost : Closeable {
fun registerCloseable(prop : Closeable)
}
class ClosableDelegateHostImpl : CloseableDelegateHost {
val closeables = arrayListOf<Closeable>()
override fun registerCloseable(prop: Closeable) {
closeables.add(prop)
}
override fun close() = closeables.forEach { it.close() }
}
class Foo : CloseableDelegateHost by ClosableDelegateHostImpl() {
private val stream by closeableLazy { FileOutputStream("/path/to/file") }
fun writeBytes(bytes: ByteArray) {
stream.write(bytes)
}
}
注意,该属性的get 方法有一个参数thisRef
.我要求它从 CloseableDelegateHost
继承,它会在关闭时关闭任何已注册的 Closeable
.为了简化实现,我将此接口委托给一个简单的基于列表的实现.
Notice, that the property's get method has a parameter thisRef
. I require that it inherits from CloseableDelegateHost
which will close any registered Closeable
s when it is closed. To simplify the implementation I delegate this interface to a simple list-based implementation.
更新(从评论中复制):我意识到,您可以将委托声明为单独的属性,然后将第二个属性委托给它.这样您就可以轻松访问委托本身.
UPDATE (copied from comments): I realized, you can just declare the delegate as a separate property and then delegate the second property to it. This way you can access the delegate itself easily.
private val streamDelegate = closeableLazy { FileOutputStream("/path/to/file") }
private val stream by streamDelegate
fun writeBytes(bytes: ByteArray) {
stream.write(bytes)
}
override fun close() {
streamDelegate.close()
}
这篇关于在 Kotlin 中访问属性委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!