我正在尝试推广使用TableQuery
来过滤掉某些filter
的功能,因此我创建了以下方法:
def fetchCoffee(coffe: String)(p: MyTable => Boolean) =
// ...
myTableQuery.filter(p)
// ...
我这样称呼它:
fetchCoffee("micoffee")(_.coffeeId == id)
但是失败,并显示此错误:
Error:(43, 29) inferred type arguments [Boolean] do not conform to method filter's type parameter bounds [T <: slick.lifted.Rep[_]]
val query = MyTable.filter(p)
Error:(43, 36) type mismatch;
found : MyTable => Boolean
required: MyTable => T
val query = myTableQuery.filter(p)
Error:(43, 35) Type T cannot be a query condition (only Boolean, Rep[Boolean] and Rep[Option[Boolean]] are allowed
val query = myTableQuery.filter(p)
我尝试将p
的类型更改为p: MyTable => Rep[Boolean]
和Rep[MyTable => Boolean]
,但仍然无法正常工作。我也尝试过将ExecutionContext
隐式传递给fetchCoffee
,而没有运气。
我应该如何编写谓词类型以使其与绑定的T
类型一致?
答案 0 :(得分:3)
尝试使用Rep[Boolean]
代替Boolean
,并使用===
代替==
。
def fetchCoffee(coffe: String)(p: MyTable => Rep[Boolean]) = {
myTableQuery.filter(p)
}
fetchCoffee("micoffee")(_.coffeeId === id)