Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

282
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda