How to define a property of a generic type with a upper bound limit?


#1

What is the Kotlin equivalent of the following Java code?

class Test {
    Class<? extends Number> types = new Class[] {
            Integer.class, Long.class, Short.class, Byte.class, Float.class, Double.class
    };
}

#2
class Test {
    val types  = arrayOf<KClass<out Number>>(
        Integer::class, Long::class, Short::class, Byte::class, Float::class, Double::class
    )
}

or if you need the java class:

class Test {
    val types  = arrayOf<Class<out Number>>(
        Integer::class.java, Long::class.java, Short::class.java, Byte::class.java, Float::class.java, Double::class.java
    )
}

for more info: https://kotlinlang.org/docs/reference/generics.html#variance or https://www.baeldung.com/kotlin-generics