HList filtered by foldRight does not provide instances

I use libraryDependencies += "com.chuusai" %% "shapeless" % "2.2.4"

I currently have HList models such as

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 would like to filter out technical sections that should not be displayed using foldRightthe one suggested in this answer :

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, defined:

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

Fold is working correctly, but suddenly I could not use the following filteror mapfor this object, for example, this code:

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

creates a compilation error:

error: could not find an implicit value for the parameter section: shapeless.ops.hlist.Partition [shapeless. :: [Header, shapeless. :: [shapeless.::►Customer,shapeless.::[Supplier,shapeless.HNil]] , shapeless.HNil.type]], Header]

bye this

view.filter[Header]

and this one

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

HNil.type foldRight

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

, , -

val hNil: HNil = HNil

?

+2
1

, , . asInstanceOf ,

view.foldRight(HNil: HNil)(collectVisRF)

, hnil, HNil, HNil.type, . Scala , , HNil.type , , Scala, None.type Nil.type .

, , , , , . , hnil, , , : , HNil () HNil (), HNil.type, .

Github, , , : -)

+3

Source: https://habr.com/ru/post/1663049/


All Articles