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?