I'm translating a very old and big Java server to Kotlin, and I'm trying to make as few logic changes as possible during the conversion. I'm looking for a way to translate this snippet of Java code without fundamentally changing it.
Consider the following toy code:
interface Pet
class Dog : Pet
class Cat : Pet
class PetSitter<T : Pet> {
fun walk(pet: T) {}
}
fun main() {
val sitters: Array<PetSitter<*>> =
arrayOf(PetSitter<Cat>(), PetSitter<Dog>())
sitters[0].walk(Cat()) // Type mismatch.
// Required: Nothing
// Found: Cat
}
This fails to compile with the message Type mismatch: inferred type is Cat but Nothing was expected
.
In Java the following works fine:
PetSitter[] sitters = ... // Note: PetSitter's generic is omitted
sitters[0].walk(new Cat());
Is it possible to similarly define a Array<PetSitter>
(with no pet generic) in Kotlin? Or some other way to make the parameter type for PetSitter<*>.walk
to be Pet
instead of Nothing
?