1

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] {}
MrX
  • 424
  • 5
  • 15
  • 1
    What line are you getting that error on? This usually happens when you try to create an instance of a generic parameter, e.g. http://stackoverflow.com/questions/20591957/class-type-required-but-t-found . – lmm Dec 31 '14 at 15:28
  • val table: TableQuery[T] = TableQuery[T] The Exception is on the following line – MrX Dec 31 '14 at 15:31
  • The question you replied with is not exactly the same as my question. While the question you posted tries to create an instance of T, all i want to do is reuse the generic inside another generic class supplied by slick. – MrX Dec 31 '14 at 16:11
  • `T` is a type parameter, not a class type. –  Dec 31 '14 at 18:53

2 Answers2

1

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.

cvogt
  • 11,260
  • 30
  • 46
0

You need define abstract TableQuery reference instead of concrete.

trait TableModel[T <: Table[_]] {

   val table: TableQuery[T]

   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] {
 val table = TableQuery[Users]
 }
Sky
  • 2,509
  • 1
  • 19
  • 28