将自定义谓词传递给 TableQuery 的过滤方法
Pass in custom predicate to TableQuery's filter method
我正在尝试概括一个函数以使用 filter
过滤掉 slick 中的一些 TableQuery
,因此我创建了以下方法:
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
?
尝试使用 Rep[Boolean]
而不是 Boolean
和 ===
而不是 ==
。
def fetchCoffee(coffe: String)(p: MyTable => Rep[Boolean]) = {
myTableQuery.filter(p)
}
fetchCoffee("micoffee")(_.coffeeId === id)
我正在尝试概括一个函数以使用 filter
过滤掉 slick 中的一些 TableQuery
,因此我创建了以下方法:
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
?
尝试使用 Rep[Boolean]
而不是 Boolean
和 ===
而不是 ==
。
def fetchCoffee(coffe: String)(p: MyTable => Rep[Boolean]) = {
myTableQuery.filter(p)
}
fetchCoffee("micoffee")(_.coffeeId === id)