Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

309
Visualizações
Is it possible to omit a type parameter in Kotlin?

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?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

In Java, this is called a raw type. It is only supported by necessity for backward compatibility, and it is discouraged to ever use, because it defeats the purpose of using generics. For the same reason, Kotlin forbids it outright because it doesn't have a pre-generics version of the language it must support.

The equivalent of using a raw type is to cast the types to be permissive. This requirement is to force you to consider if the cast is appropriate.

(sitters[0] as PetSitter<Cat>).walk(Cat())
over 4 years ago · Santiago Trujillo Relatório

0

Update: see https://stackoverflow.com/a/70114733/2875073 which is simpler.

It seems Kotlin doesn't allow raw types, but it looks like a work-around is to cast my instance of Pet to Pet and then make a when case enumerating each subtype and casting sitters[0] for each case:

val myPet: Pet = Cat()
when (myPet) {
    is Dog -> (sitters[0] as PetSitter<Dog>).walk(myPet)
    is Cat -> (sitters[0] as PetSitter<Cat>).walk(myPet)
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda