user8459720
user8459720

Reputation: 385

What is the syntax for Class< ? extends class_name> in kotlin?

I am trying to make an Arraylist that accepts any class variable that inherited from Word_Class but it doesn't work :

var lst = ArrayList<Class<Word_Class>>();
lst.add(Class<Noun_Class>);

I am looking for the syntax for Class< ? extends class_name> in kotlin  

Upvotes: 16

Views: 5212

Answers (1)

holi-java
holi-java

Reputation: 30696

You can use the out type projection in Kotlin. it is equivalent to ? extends T in Java, for example:

//                         v--- out type projection
var lst = ArrayList<Class<out Word_Class>>()

To get a Java class you should use KClass#java, for example:

//                 v--- get a KClass instance
lst.add(Noun_Class::class.java)
//                         ^--- get java.lang.Class instance 

Upvotes: 21

Related Questions