我在用着libraryDependencies += "com.chuusai" %% "shapeless" % "2.2.4"
目前我有模型 HList 类型,例如
sealed trait Section
case class Header(...) extends Section
case class Customer(...) extends Section
case class Supplier(...) extends Section
case class Tech(...) extends Section
type ContractView = Header :: (Customer :: Supplier :: HNil) :: Tech :: HNil
在我的用户代码中,我想过滤不应该查看的技术部分foldRight
按照提议在这个答案中 https://stackoverflow.com/a/19894035/210905:
trait collectAllRF extends Poly2 {
implicit def atAny[L <: HList, X] = at[X, L](_ :: _)
}
object collectVisRF extends collectAllRF {
implicit def atInvis[L <: HList, S <: Section : InvisibleSection] = at[S, L]((_, l) => l)
}
例如有定义:
trait InvisibleSection[S <: Section]
implicit object _techisInvisible extends InvisibleSection[Tech]
折叠工作正常,但突然我无法使用以下内容filter
or map
在此对象上,例如以下代码:
val filtered = view.foldRight(HNil)(collectVisRF)
view.filter[Header]
产生编译错误:
错误:找不到参数分区的隐式值:
shapeless.ops.hlist.Partition[shapeless.::[标头,shapeless.::[shapeless.::[客户,shapeless.::[供应商,shapeless.HNil]],shapeless.HNil.type]],标头]
而这
view.filter[Header]
和这个
val h = view.select[Header]
val l = view.select[Customer::Supplier::HNil]
val c = l.select[Customer]
val s = l.select[Supplier]
val manual = h :: (c :: s :: HNil) :: HNil
manual.filter[Header]
编译正常
最近我发现很少HNil.type
结束时foldRight
的结果类型并将我的过滤器定义更改为
view.foldRight(HNil.asInstanceOf[HNil])(collectVisRF)
一切正常
这是预期的行为吗?如果是,为什么没有一些
val hNil: HNil = HNil
在图书馆?