https://kotlinlang.org logo
#getting-started
Title
# getting-started
z

zsperske

12/23/2020, 6:19 PM
if you have a static java method like:
Copy code
public static <T> MyFactory<T> of(Class<T> baseType) { ... }
and you wanted to write a kotlin extension method like:
Copy code
fun <T> MyFactory<T>.withSubTypes(types: List<Class<T>) {
   var factory = this
   types.forEach { factory = factory.withSubType(it) }
   return factory
}
How do you properly type the static method? If you write something like:
Copy code
val factory = MyFactory.of(MyClass::class.java)
              .withSubTypes(listOf(...))
You'll get a syntax error on your list:
Type mismatch. Required: Nothing Found: MyClass
. Adding explicit type arguments doesn't help.
v

Vampire

12/23/2020, 7:03 PM
Maybe it should be
fun <T> MyFactory<T>.withSubTypes(types: List<Class<T>) {
?
z

zsperske

12/23/2020, 7:12 PM
Ah my mistake, my actual code does have that
I believe I solved it, I needed my
types: List<Class<T>
to be
types: List<Class<out T>
3 Views