将自定义谓词传递给TableQuery的过滤器方法

时间:2019-10-06 17:58:30

标签: scala slick

我正在尝试推广使用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类型一致?

1 个答案:

答案 0 :(得分:3)

尝试使用Rep[Boolean]代替Boolean,并使用===代替==

def fetchCoffee(coffe: String)(p: MyTable => Rep[Boolean]) = {
  myTableQuery.filter(p)
}

fetchCoffee("micoffee")(_.coffeeId === id)