https://kotlinlang.org logo
Title
m

mbonnin

10/26/2019, 12:30 PM
Anyway I can make this work somehow ?
fun <T> foo(aClass: java.lang.Class<T>): MutableList<T> {
  return mutableListOf()
}

inline fun <reified T:Any> bar(t: T): MutableList<T> {
  // fails here: expected: MutableList<T>, found: MutableList<out T>
  return foo(t::class.java) 
}
k

karelpeeters

10/26/2019, 1:06 PM
Not really, it just doesn't make sense:
t
could have a subtype of
T
, and so
t::class.java
could be a subclass of
Class<T>
and a
MutableList
is invariant. The only thing you can do is make
bar
return
MutableList<out T>
which is correct.
m

mbonnin

10/26/2019, 1:16 PM
t
could have a subtype of
T
right, thanks !