Search code examples
scalaslicktypesafe

Class Type T - Explained


Can anyone explain too me why I get the following compiler exception while declaring the TableQuery inside my trait.

class type required but T found

Isn't the T actually a Class type or am I mistaken?

trait TableModel[T <: Table[_]] {
  val table: TableQuery[T] = TableQuery[T]    <~~~~~~~~~~ class type required but T found

  def exists(implicit session: Session): Boolean =
    (!MTable.getTables(table.baseTableRow.tableName).list.isEmpty)

  def schemaDescription: MySQLDriver.SchemaDescription = table.ddl

  def create(implicit session: Session): Unit =  schemaDescription.create

  def drop(implicit session: Session): Unit =  schemaDescription.drop
}


object UsersTable extends TableModel[Users] {}

Solution

  • The reason that you get this error message is that TableQuery[T] in value position is actually TableQuery.apply[T] which is a macro that expands into new TableQuery(new T(_)). It is not certain from only the type constraint T <: Table[_] that T is a non-abstract class that you can call new on (public constructor). The actual error message scalac gives here isn't very precise here but in the right ballpark.