2

我正在尝试使用 shapeless 对Hlists 进行添加和删除。但我似乎无法让它工作。

所以我在这里是我的清单:

object ShapelessExample {

    import shapeless._

    def main(args: Array[String]): Unit = {

        case class Container[T <: Singleton](name: T)

        val a = Container("A") :: Container("B") :: Container("C") :: HNil
        val b = Container("B") :: Container("C") :: HNil

        println {
            a.removeAll[b.type] //doesn't work
        }
    }
}

所以removeAllon 方法Hlist只需要一个类型参数,但我似乎无法使用b.type. 我可以手动指定a.removeAll[Container["B"] :: Container["C"] :: HNil],但有什么办法只使用b's 类型?

4

1 回答 1

4

Shapeless 试图删除精确的类型b.type,但在其中找不到它,Container["A"] :: Container["B"] :: Container["C"] :: HNil所以@user是正确的,单例类型b.type太具体了。

为了HListval单例类型推断类型尝试修改方法

implicit class RemoveAllOps[L <: HList](a: L) {
  def removeAll[L1 <: HList](b: L1)(implicit
    ra: shapeless.ops.hlist.RemoveAll[L, L1]
  ): ra.Out = ra(a)
}

a.removeAll(b) // (Container(B) :: Container(C) :: HNil,Container(A) :: HNil)

或者

implicit class RemoveAllOps[L <: HList](a: L) {
  def removeAllFrom[O <: Singleton] = new {
    def apply[L1 >: O <: HList]()(implicit
      ra: shapeless.ops.hlist.RemoveAll[L, L1]
    ): ra.Out = ra(a)
  }
}

a.removeAllFrom[b.type]() //(Container(B) :: Container(C) :: HNil,Container(A) :: HNil)
于 2020-10-18T19:32:48.820 回答