如何在Kotlin中嵌套多个属性委托

如何在Kotlin中嵌套多个属性委托,kotlin,delegates,delegation,kotlin-delegate,Kotlin,Delegates,Delegation,Kotlin Delegate,我遇到了一个案例,我想“链接”多个代理(将一个代理的输出传输到另一个代理) 这似乎是可能的: private val errorLogList by listSO(listOf()、SODest.NONE、publicSOAccessRights()) val errorLog:addToSet的状态对象(errorLogList) 但是,这看起来不太好:)。我想用这样一句话来表达: val errorLog:StateObject by addToSet( listSO(listOf(),S

我遇到了一个案例,我想“链接”多个代理(将一个代理的输出传输到另一个代理)

这似乎是可能的:

private val errorLogList by listSO(listOf()、SODest.NONE、publicSOAccessRights())
val errorLog:addToSet的状态对象(errorLogList)
但是,这看起来不太好:)。我想用这样一句话来表达:

val errorLog:StateObject by addToSet(
listSO(listOf(),SODest.NONE,publicSOAccessRights())
)
我的问题:在Kotlin中是否可以通过委托创建此类属性?

以下是我的委托的两个实现:

addToSet:

开放类ChildSOReturner{
val set:set=setOf()
inline fun addToSet(so:T)=对象:ReadOnlyProperty{
重写运算符fun getValue(thisRef:Any?,属性:KProperty):T{
if(thisRef是T){
set.plus(so)
如此回报
}else抛出IllegalArgumentException()
}
}
}
listSo:

fun listSO(
初始状态:列表,
索迪斯:索迪斯,
soAccessRights:soAccessRights
)=对象:只读属性{
重写运算符fun getValue(thisRef:Any?,属性:KProperty):StateObject{
val meta=SOMeta(SOId(property.name)、soDest、soAccessRights)
return StateObjectList(initialState,meta)
}
}

结果证明这很棘手,但也有可能(除非我遗漏了一些东西,而且没有经过测试,但这个想法应该可行):


相关:有没有一种方法可以将ComposeProperty(..{addToSet(it)}组合到像AddComposited(..)这样的调用中?我似乎不明白。
fun <T, U, V> composeProperties(prop: ReadOnlyProperty<T, U>, f: (U) -> ReadOnlyProperty<T, V>) : ReadOnlyProperty<T, V> {
    var props = mutableMapOf<Pair<T, KProperty<*>>, ReadOnlyProperty<T, V>>()
    return object : ReadOnlyProperty<T, V> {
        override operator fun getValue(thisRef: T, property: KProperty<*>): V {
            val prop1 = props.getOrPut(Pair(thisRef, property)) { 
                f(prop.getValue(thisRef, property))
            }
            return prop1.getValue(thisRef, property)
        }
    }
}
val errorLog: ... by composeProperties(listSO(...)) { addToSet(it) }