Home > Software engineering >  Generics and inline fuctions in Kotlin
Generics and inline fuctions in Kotlin

Time:01-26

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?

CodePudding user response:

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

  •  Tags:  
  • Related