Hi there!
I’ve been trying to create a generic DAO using Room database.
Usually a simple implementation looks like this:
@Entity
class User(
@PrimaryKey(autoGenerate = true)
val id: Int
val name: String
)
and
@Dao
interface UserDao{
@Query("SELECT * FROM user")
fun getAll() : List<User>
}
What I was trying to do, is to introduce a generic Dao and Entity
interface BaseEntity{
abstract const val ENTITY_NAME
}
@Dao
interface BaseDao< T : BaseEntity>{
@Query("SELECT * FROM ${T.ENTITY_NAME}")
fun getAll() : List<T>
}
As you sure know, it’s not possible to declare an abstract const val, as opposed to abstract val/var.
How can I force the implementing class to override a certain value, in a way that the compiler would treat it as a compile-time constant? I don’t belive it to be possible in Java either. (C++ constexpr maybe?)