使用无形来实现过滤器

时间:2016-11-07 11:10:05

标签: scala filter shapeless

我正在尝试实施一个过滤器,例如本gist中描述的过滤器(请参阅上一条评论)。

我想将沙拉碗包装成水果类,如下所示:

scala> class Fruits[L <: HList](val bowl: FruitBowl[L])

然后我想在列表中创建多个水果,如下所示:

scala> val fruits = List(new Fruits(new FruitBowl(Apple() :: Pear() :: HNil)), new Fruits(new FruitBowl(Pear() :: HNil)))

然后我只想获得每个水果的苹果部分,所以我尝试过滤:

scala> fruits.map(_.bowl.getAll[Apple])

但后来我收到以下错误:

<console>:16: error: could not find implicit value for parameter pf: shapeless.ops.hlist.Partition[_2,Apple]
   fruits.map(_.bowl.getAll[Apple])

但我认为分区应该是通用的,所以我认为没有理由不应该使用它......

我不明白这里有什么意思。知道出了什么问题吗?

0 个答案:

没有答案