鉴于以下情况,我相信,输入class:
trait Element[A, B] {
val input: A
val filteredValue: Option[B]
}
然后我定义filterFn
来过滤输入queryFilterValues
,然后过滤咖喱List[A]
,返回List[A]
,即过滤了什么:< / p>
def filterFn[A, B](queryFilterValues: List[B])
(implicit ev: A => Element[A, B]): List[A] => List[A] =
elements => {
queryFilterValues match {
case _ :: _ => elements.flatMap { e =>
ev(e).filteredValue match {
case Some(v) => if(queryFilterValues.contains(v) ) List(e.input) else Nil
case None => List.empty
}
}
case Nil => elements.map(_.input)
}
}
然后,我创建了一个Person
以及一个Person => Element[Person, String]
实例:
case class Person(name: Option[String])
object Person {
implicit def personToElement(p: Person) = new Element[Person, String] {
val input = p
val filteredValue = p.name
}
}
最后,我尝试使用它:
// Filter on these names
val nameFilters = List( "jane", "joe", "will" )
val joe = Person( Some("joe") )
// Expect to get a `List( joe )` back since `joe#name` exists in the list.
scala> filterFn( nameFilters )( List[Person](joe) )
但是我得到以下编译时错误:
<console>:20: error: type mismatch;
found : List[Person]
required: ? => Element[?,String]
filterFn( nameFilters )( List[Person](joe) )
答案 0 :(得分:3)
filterFn的签名是
def filterFn[A, B](queryFilterValues: List[B])
(implicit ev: A => Element[A, B])
当你用
打电话时filterFn( nameFilters )( List[Person](joe) )
你作为第二个参数传递List[Person](joe)
。但是您定义的签名需要从A
到Element[A, B]
这就是你看
的原因 found : List[Person]
required: ? => Element[?,String]