# Variant Generics (Covariance / \`out\`) with Java call site and templated type

**URL:** <https://discuss.kotlinlang.org/t/variant-generics-covariance-out-with-java-call-site-and-templated-type/25371>\
**Category:** Support\
**Created:** [August 2, 2022, 9:46pm UTC](https://discuss.kotlinlang.org/t/variant-generics-covariance-out-with-java-call-site-and-templated-type/25371 "2022-08-02T21:46:32Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![groundlar](https://avatars.discourse-cdn.com/v4/letter/g/57b2e6/32.png) [@groundlar](https://discuss.kotlinlang.org/u/groundlar)\
**Post date:** [August 2, 2022, 9:46pm UTC](https://discuss.kotlinlang.org/t/variant-generics-covariance-out-with-java-call-site-and-templated-type/25371/1 "2022-08-02T21:46:33Z")

</div>

In converting some Java code, I wanted to translate something like

```auto
public class TemplatedType<T> { /* ... */ }
// In some other class:
public List<ReturnType> foo(Class<? extends TemplatedType> subType) { /* ... */ }
public List<ReturnType> foo(Collection<Class<? extends TemplatedType>> subTypes) { /* ... */ }

```

with a use-site variance like (for the function using collections)

```auto
List<? extends TemplatedType> typeList = /* Init code */ ;
foo(typeList);

```

My first pass matched the Intellij auto-conversion:

```auto
fun foo(subType: Class<out TemplatedType<*>>): List<ReturnType> { /* ... */ }
fun foo(subTypes: Collection<Class<out TemplatedType<*>>>): List<ReturnType> { /* ... */ }

```

Interestingly, usages of the former (single class arg) worked perfectly while the latter failed building as it was unable to convert the type, something like:

> error: no suitable method found for foo(List\<Class\<? extends TemplatedType\>\>)
> 
> ```
> method foo(Class<? extends TemplatedType<?>>) is not applicable
> (argument mismatch; List<Class<? extends TemplatedType>> cannot be converted to Class<? extends TemplatedType<?>>)
> method foo(Collection<? extends Class<? extends TemplatedType<?>>>) is not applicable
> (argument mismatch; List<Class<? extends TemplatedType>> cannot be converted to List<? extends Class<? extends TemplatedType<?>>>)
> 
> ```

I ended up using upper bounds, which worked (and makes sense):

```auto
fun <T : TemplatedType<*>> foo(subType: Class<out T>): List<ReturnType> { /* ... */ }
fun <T : TemplatedType<*>> foo(subTypes: Collection<Class<out T>>): List<ReturnType> { /* ... */ }

```

However I don’t fully understand the compatibility issue or the difference in bounding. I took a look at the [Variant Generics](https://kotlinlang.org/docs/java-to-kotlin-interop.html#variant-generics) section of the docs, in addition to the obvious [Generics](https://kotlinlang.org/docs/generics.html) section, but still feel I’m missing something.

Can someone break this down a little for me?
