everyone! I have some class from Java dependency:
public class MyClass {
public MyClass copy() {
....
}
public void init() {
...
}
}
And I want to write a function in my Kotlin class:
fun <T : MyClass> prepareListCopy(objects: List<T>?): List<T>? =
objects?.map { it.copy().apply { init() } }
I want my function to accept and return List of subtypes of MyClass, but IDE shows me this error: Change return type of enclosing function 'KOtlinClass.prepareListCopy' to 'List< MyClass >?'
How can I correctly define generic in this function?
CodePudding user response:
In the definition you provided, MyClass.copy()
returns MyClass
. So even when you have a subtype (say MySubClass
) and you call MySubClass.copy()
you will still get a MyClass
, not a MySubClass
.
This is why you cannot say your generic function returns a List<T>?
, you have to say it returns List<MyClass>?
:
fun <T : MyClass> prepareListCopy(objects: List<T>?): List<MyClass>? =
objects?.map { it.copy().apply { init() } }
(which is exactly what the error message is telling you :D)
Now given what this function does, there is no real point in knowing the exact T
anymore, you could just say you want the input list to contain subtypes of MyClass
by using List<out MyCLass>
, and the function doesn't need to be generic:
fun prepareListCopy(objects: List<out MyClass>?): List<MyClass>? =
objects?.map { it.copy().apply { init() } }