Abstract const val

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?)

I understand what you wanted to accomplish, but I’m afraid inheritance and compile-time constants simply cannot be mixed. The string "SELECT * FROM ${T.ENTITY_NAME}" doesn’t even know that T exists.

This feature is already built in Spring Data repositories (if you are using Spring ofcourse). Just write:

@Query("SELECT p FROM #{#entityName} p")
fun getAll(): List<T>