为什么将KClass声明为KClass <T:Any>而不是KClass <T>,以便type参数可以为空

And*_*nov 3 java generics kotlin

我试图在Kotlin中基于java数组实现检查 。但是我将KClass与允许空值的通用参数类型一起使用时遇到问题。Stack<E><E>

Java泛型类型在运行时不可用,但数组类型可用。我想使用此功能,以便在运行时进行内置类型检查。

有关已检查/未检查的更多详细信息,可以在这里找到/sf/answers/37120261/

interface Stack<E> {
    fun push(elem: E)
    fun pop(): E
}
Run Code Online (Sandbox Code Playgroud)
class CheckedStack<E>(elementType: Class<E>, size: Int) : Stack<E> {

    companion object {
        inline fun <reified E> create(size: Int): CheckedStack<E> {
            //**compile error here**
            return CheckedStack(E::class.javaObjectType, size)
        }
    }

    @Suppress("UNCHECKED_CAST")
    private val array: Array<E?> = java.lang.reflect.Array.newInstance(elementType, size) as Array<E?>
    private var index: Int = -1

    override fun push(elem: E) {
        check(index < array.size - 1)
        array[++index] = elem
    }

    override fun pop(): E {
        check(index >= 0);
        @Suppress("UNCHECKED_CAST")
        return array[index--] as E
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望这段代码可以这样工作:

fun main() {
    val intStack = CheckedStack.create<Int>(12) // Stack must store only Integer.class values
    intStack.push(1); //[1]
    intStack.push(2); //[1, 2]

    val stackOfAny: Stack<Any?> = intStack as Stack<Any?>;
    stackOfAny.push("str") // There should be a runtime error
}
Run Code Online (Sandbox Code Playgroud)

但是我有编译错误

Error:(39, 42) Kotlin: Type parameter bound for T in val <T : Any> KClass<T>.javaObjectType: Class<T>
 is not satisfied: inferred type E is not a subtype of Any
Run Code Online (Sandbox Code Playgroud)

为了修复它,我需要绑定type参数,<E : Any>但是我需要使堆栈能够使用可为null的值<T : Any?>。如何解决?

为什么KClass声明为KClass<T : Any>not KClass<T : Any?>

UPD:它的工作原理,如果使用E::class.java,而不是E::class.javaObjectType 因为房地产val <T> KClass<T>.java: Class<T>有Param类型<T>与注解@Suppress("UPPER_BOUND_VIOLATED")

但是该属性val <T : Any> KClass<T>.javaObjectType: Class<T>具有type <T : Any>

就我而言,Kotlin将Int编译为Integer.class而不是int(就我而言)。但是我不确定它是否也能正常工作。

Ban*_*non 5

可空类型本身不是类,因此它们没有类对象。这就是为什么KClasstype参数具有Any上限的原因。

您可以调用::class.java可为null的类型,但是它将被评估为与对相应非null类型的相同调用相同的类对象。所以,如果您更换E::class.javaObjectTypeE::class.java,元素的类型将在运行时检查,但没有空支票就搞定。

如果需要空检查,则可以自己添加它们。我还建议将数组创建移至工厂方法。这是您可以执行的操作:

class CheckedStack<E>(private val array: Array<E?>, private val isNullable: Boolean) : Stack<E> {

    companion object {
        // This method invocation looks like constructor invocation
        inline operator fun <reified E> invoke(size: Int): CheckedStack<E> {
            return CheckedStack(arrayOfNulls(size), null is E)
        }
    }

    private var index: Int = -1

    override fun push(elem: E) {
        if (!isNullable) elem!!
        check(index < array.size - 1)
        array[++index] = elem
    }

    override fun pop(): E {
        check(index >= 0)
        @Suppress("UNCHECKED_CAST")
        return array[index--] as E
    }
}
Run Code Online (Sandbox Code Playgroud)