BaseDao which should have some generic methods for all the child Dao (UserDao, MovieDao, etc). class UserDao : BaseDao<UserEntity>() { ... }
This base class implements the generic methods as follows:
open class BaseDao<T: Any>() { fun get(id: String): T? {
return getCollection().findOneById(id)}fun save(entity: T): T {return getCollection().save(entity)}fun delete(id: String) {getCollection().deleteOneById(id)}...
}
However, a problem occurs on getCollection() method:
private inline fun <reified T: Any> getCollection(): MongoCollection<T> {
return MongoDb.getDatabase().getCollection<T>()
}
This gets a compilation error each time I call it:
Type inference failed: Not enough information to infer parameter T in
inline fun <reified T : Any> getCollection(): MongoCollection<T#1 (type parameter of app.api.db.dao.BaseDao.getCollection)>
Please specify it explicitly.
Any idea how can I achieve this generic getCollection() in BaseDao?