Reputation: 507
I'm playing around with slick 3.0.0's new DBIO api but am having some problem with generics.
With this code:
import scala.concurrent._
import slick.driver.MySQLDriver.api._
import slick.lifted.AbstractTable
object Crud {
def fetchById[T: AbstractTable[R], R](db: Database, table: TableQuery[T], id: Int, rowId: T => Rep[Int]): Future[Option[T]] = {
val fetchByIdQuery: Query[T, R, Seq] = table.filter(row => rowId(row) === id)
db.run(fetchByIdQuery.result)
}
}
The compiler says that AbstractTable does not take any Type parameters. I don't understand this with a declaration as seen here https://github.com/slick/slick/blob/3.0/slick/src/main/scala/slick/lifted/AbstractTable.scala.
slick.lifted.AbstractTable[R] does not take type parameters
def fetchById[T: AbstractTable[R], R](db: Database, table: TableQuery[T], id: Int, rowId: T => Rep[Int]): Future[Option[T]] = {
I suspect this is also the cause of this error.
type mismatch;
found : slick.lifted.Query[T,T#TableElementType,Seq]
required: slick.driver.MySQLDriver.api.Query[T,R,Seq]
(which expands to) slick.lifted.Query[T,R,Seq]
val fetchByIdQuery: Query[T, R, Seq] = table.filter(row => rowId(row) === id)
Any advice?
Upvotes: 4
Views: 1215
Reputation: 1108
Try it this way
T <: AbstractTable[R] // instead of T : AbstractTable[R]
so it should look like below
Object Crud {
def fetchById[T <: AbstractTable[R], R](db: Database, table: TableQuery[T], id: Int, rowId: T => Rep[Int])
: Future[Option[T#TableElementType]] = {
val fetchByIdQuery = table.filter(row => rowId(row) === id)
db.run(fetchByIdQuery.result.headOption)
}
}
Upvotes: 1