我在我的代码中找到了一个重复的模式,我想对它进行抽象,但是我似乎无法正确理解它。我想抽象的代码是这样的:
def queryByPredicate(p: (OAuthClientTable => Rep[Boolean])): Future[Option[OAuthClient]] = {
val query = oAuthClientTableQuery.filter(p)
db.run(query.result.headOption)
}
我试过这样的事情:
def queryByPredicate[A, B](p: (A => Rep[Boolean]), t: TableQuery[A]): Future[Option[B]] = {
val query = t.filter(p)
db.run(query.result.headOption)
}
然而,编译器抱怨以下错误:
Error:(132, 25) type mismatch;
found : slick.profile.SqlAction[Option[A#TableElementType],slick.dbio.NoStream,slick.dbio.Effect.Read]
required: slick.dbio.DBIOAction[Option[B],slick.dbio.NoStream,Nothing]
db.run(query.result.headOption)
老实说,我不知道如何创建通用的Option[A#TableElementType]
答案 0 :(得分:1)
def queryByPredicate[A <: Table[B], B](p: (A => Rep[Boolean]), t: TableQuery[A]): Future[Option[B]] = {
val query = t.filter(p)
db.run(query.result.headOption)
}