简体   繁体   English

由foldRight过滤的HList不提供实例

[英]HList filtered by foldRight is not providing instances

I'm using libraryDependencies += "com.chuusai" %% "shapeless" % "2.2.4" 我正在使用libraryDependencies += "com.chuusai" %% "shapeless" % "2.2.4"

Currently i have model HList types like 目前我有模型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

In my user code, i'd like to filter technical sections that are not supposed to view using foldRight as proposed in this answer : 在我的用户代码中,我想按照本答案中的建议过滤不应使用foldRight查看的技术部分:

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)
}

For example there are defined: 例如,定义了:

trait InvisibleSection[S <: Section]
implicit object _techisInvisible extends InvisibleSection[Tech]

Fold is working correctly, but suddenly i could not use following filter or map on this object, so for example this code: 折叠工作正常,但突然我无法在此对象上使用以下filtermap ,例如此代码:

val filtered = view.foldRight(HNil)(collectVisRF)
view.filter[Header] 

produces compile error: 产生编译错误:

error: could not find implicit value for parameter partition: shapeless.ops.hlist.Partition[shapeless.::[Header,shapeless.::[shapeless.::[Customer,shapeless.::[Supplier,shapeless.HNil]],shapeless.HNil.type]],Header] 错误:找不到参数分区的隐含值:shapeless.ops.hlist.Partition [shapeless。:: [Header,shapeless。:: [shapeless。::[Customer,Shapeless。:::Supplier ,shapeless.HNil]] ,shapeless.HNil.type],头]

while this 而这个

view.filter[Header]

and this 还有这个

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]

compiles ok 编译确定

Lately i've found little HNil.type at end of foldRight 's result type and changed my filter definition to 最近我在foldRight的结果类型的末尾发现了一些HNil.type ,并将我的过滤器定义更改为

view.foldRight(HNil.asInstanceOf[HNil])(collectVisRF)

And all worked properly 一切正常

Is this an expected behaviour, and if yes why there is no some 这是一种预期的行为,如果是,为什么没有一些

val hNil: HNil = HNil

In the library? 在图书馆?

Your eventual fix is almost, but not quite, right. 你的最终修复几乎是,但并不完全正确。 Rather than asInstanceOf you should use a type ascription, 而不是asInstanceOf你应该使用类型归属,

view.foldRight(HNil: HNil)(collectVisRF)

Your question about why there is no definition of an hnil value typed as HNil rather than as HNil.type is a good one. 关于为什么没有hnil值定义为HNil而不是HNil.type是一个很好的问题。 shapeless is different from typical Scala libraries in that it makes heavy use of singleton types, HNil.type included, so the current situation isn't as obviously wrong as the corresponding situation in the Scala standard library where None.type and Nil.type are almost never desired. shapeless与典型的Scala库的不同之处在于它大量使用了包含HNil.type的单例类型,因此当前情况并不像Scala标准库中的相应情况那样明显错误,其中None.typeNil.type是几乎从未想过。

Nevertheless the situation you describe in your question comes up more often than I would like, so it's clearly a real problem. 然而,你在问题中描述的情况比我想象的更频繁,所以这显然是一个真正的问题。 I think it would be too confusing to have two hnil values, one with a more precise type than the other, so the question boils down to: how much existing stuff would break if HNil (the type) was inferred as the type of HNil (the value) rather than HNil.type as it is now. 我认为有两个hnil值太混乱了,一个比另一个更精确的类型,所以问题归结为:如果HNil (类型)被推断为HNil的类型,现有的东西会破坏HNil (价值)而不是现在的HNil.type

Feel free to open a ticket in the shapeless issue tracker on Github to investigate this, and if you'd like to give it a try, please do :-) 随意在Github上的无形问题跟踪器中打开一张票来调查此问题,如果你想尝试一下,请执行:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM