简体   繁体   中英

Converting Java wildcard type argument to kotlin

Trying to learn Kotlin and on one of my classes in Java I have a value that is a list that contains any class that extends a certain interface as seen below.

List<? extends foo>

I have previously tried the out modifier example:

interface foo 
{
   val bar: out List
}

and passing the class through the modifier example:

interface foo<T> 
{ 
   val bar: List<T> 
}

The result wanted is that if two classes extended this interface they could both have this value but one could have a list of type A and one of list type B

The Kotlin equivalent to Java List<? extends foo> List<? extends foo> doesn't depend on what foo looks like and doesn't require changing it. It's MutableList<out foo> ( https://kotlinlang.org/docs/reference/generics.html#use-site-variance-type-projections ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM