0

所以,我正在研究一个涉及自动写入ByteBuffer类扩展内容的概念Bufferizable

abstract class Bufferizable {

    abstract val fieldOrder: Array<String>

    open fun size(): Int = TODO()

    var initialized = false

    open infix fun to(address: Long) {

        if (initialized) {

            addFunctions.forEach {  }

        } else {

            addFunctions = Array(fieldOrder.size) { null }
            members = Array(fieldOrder.size) { null }

            fieldOrder.forEachIndexed { i, field ->
                val member = this::class.declaredMemberProperties.find { it.name == field }!!
                addFunctions[i] = when (member.returnType) {
                    Mat4::class.defaultType.javaType -> WithAddress::addMat4
                    ...
                    else -> throw Error()
                } as BufferizableAddFunctionType
                members[i] = member.get(this) // error
            }

            initialized = true
        }
    }

    infix fun from(address: Long): Unit = TODO()

    var addFunctions = arrayOf<BufferizableAddFunctionType?>()
    var members = arrayOf<Any?>()
}

typealias BufferizableAddFunctionType = (Any) -> Unit

object WithAddress {

    var address = NULL
    var offset = 0

    fun addMat4(mat4: Mat4) {
        for (i in 0..3)
            for (j in 0..3) {
                memPutFloat(address + offset, mat4[i, j])
                offset += Float.BYTES
            }
    }
    ...
}

这个想法是声明,例如,以下

object uboVS : Bufferizable() {

    var projectionMatrix = Mat4()
    var modelMatrix = Mat4()
    ...

    override val fieldOrder = arrayOf("projectionMatrix", "modelMatrix", "viewMatrix")
}

然后每当uboVS to address第一次调用时,它都会被初始化,然后按照指定的顺序将其内容写入本机地址。

但是我唯一遇到的问题是收集成员以稍后作为参数传递给第 i 个addFunction

因为编译器在这里抱怨:

members[i] = member.get(this)

错误:(328, 37) Kotlin: Out-projected type 'KProperty1' 禁止使用'public abstract fun get(receiver: T): R defined in kotlin.reflect.KProperty1'

我该如何解决?

4

1 回答 1

4

这是一个继承问题。您的反射调用this::class.declaredMemberProperties.find{ ... }返回一个KProperty1<out Bufferizable, Any?>这意味着一个从 Bufferizable 继承的东西的属性(有关更多信息,请参见声明站点差异out)。通过编译器不知道该属性属于哪个类,它不允许直接访问。要解决这个问题,您必须访问 getter 函数:

members[i] = member.getter.call(this)

通过 getter 函数属于对象而不属于反射 api,它遵循默认的继承行为并且可以被调用。

于 2018-05-01T12:16:00.613 回答