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

283
Visualizações
Generics and inline fuctions in Kotlin

I have difficulties with generics in Kotlin. I want to make universal interface and implementation and several type-specific DAOs.

// Type parametrized DAO
interface DaoInterface<T : Any> {
    fun <T : Any> getByCollectionId(collectionId: Long, clazz: Class<T>): List<T>
}

//Generic DAO implementation
open class DaoInterfaceImpl<T : Any>(private val entityManager: EntityManager) : DaoInterface<T> {
    override fun <T : Any> getByCollectionId(collectionId: Long, clazz: Class<T>): List<T> =
        entityManager.createQuery(
            "from ${clazz.name} c where c.collectionId = :collectionId", clazz
        )
            .apply { setParameter("collectionId", collectionId) }
            .resultList ?: listOf()

    inline fun <reified T : Any> getByCollectionId(collectionId: Long): List<T> =
        getByCollectionId(collectionId, T::class.java)
}

//Type specified DAO
class TestContactDao(entityManager: EntityManager) : DaoInterface<Contacts>, DaoInterfaceImpl<Contacts>(entityManager)

class Test(private val testContactDao: TestContactDao) {
    fun call() {
        val result1 = testContactDao.getByCollectionId<Contacts>(123L)
        val result2 : List<Contacts> = testContactDao.getByCollectionId(123L)
        val result3 = testContactDao.getByCollectionId(123L)
    }
}

result1 and result2 works fine, but in result3 I get "Type inference failed" error.

I don't want to specify generic in theese calls(like in result1 and 2). I want to make several DAO's for different classes(Contacts, Adresses and others) and call them like in result3.

How can i do this?

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

0

The problem ist that your reified T does not match the class type T, as you redefine that type variable. You can still avoid having to pass a Class by defining an extension function on the interface:

// helper extension to simplify access
inline fun <reified T> DaoInterface<T>.getByCollectionId(collectionId: Long): List<T> = getByCollectionId(collectionId, T::class.java)

Why does this work? Here we define T as reified and specify that DaoInterface has to be of the same type T.

I have a playground example that's working here: https://pl.kotl.in/kLiM9xD3y

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