1

我遇到过一种情况,我想“链接”多个代表(将一个代表的输出连接到另一个代表)。

这似乎是可能的:

private val errorLogList by listSO(listOf<StateObject<Luxeption>>(), SODest.NONE, publicSOAccessRights())
val errorLog: StateObject<List<StateObject<Luxeption>>> by addToSet(errorLogList)

但是,这看起来不太好:)。我想像这样在一行中做到这一点:

val errorLog: StateObject<List<StateObject<Luxeption>>> by addToSet(
   listSO(listOf<StateObject<Luxeption>>(), SODest.NONE, publicSOAccessRights())
)

我的问题:在 Kotlin 中是否可以通过委托创建这种类型的属性?


这是我的代表的两个实现:

添加到设置:

open class ChildSOReturner {

    val set: Set<StateObject<*>> = setOf()

    inline fun <reified T> addToSet(so: T) = object: ReadOnlyProperty<Any?, T> {
        override operator fun getValue(thisRef: Any?, property: KProperty<*>): T {
            if (thisRef is T) {
                set.plus(so)
                return so
            } else throw IllegalArgumentException()
        }
    }
}

列表所以:

fun <O> listSO(
    initialState: List<StateObject<O>>,
    soDest: SODest,
    soAccessRights: SOAccessRights
) = object : ReadOnlyProperty<Any?, StateObject<List<StateObject<O>>>> {

    override operator fun getValue(thisRef: Any?, property: KProperty<*>): StateObject<List<StateObject<O>>> {
        val meta = SOMeta(SOId(property.name), soDest, soAccessRights)
        return StateObjectList(initialState, meta)
    }

}
4

1 回答 1

1

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

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) }
于 2020-02-15T22:01:31.313 回答