Type-level Shapeless : aggregating a HList type elements

好久不见. 提交于 2019-12-06 08:03:01

问题


I want to fold a HList with this Monad but at the type-level

trait TypeMonad{

  type Append[A,B] = A with B

  type Identity = Any
}

Hence, the HList : "A :: B:: C :: HNil " would give the type " A with B with C with Any"

Quite easy to do if i had implemented HList :

sealed trait HList {
  type Fuse
}
final trait HCons[H,L<:HList] extends HList {
  type Fuse = H with L#Fuse
}
final trait HNil extends Hlist {
  type Fuse = Any
}

However I don't know how to have this functionality in the shapeless environment

My use case is the following :

I need to restrain implicit class use to certain class by constraining the valid parameters For example :

trait Filter
trait IC1Filter extends Filter
trait IC2Filter extends Filter
(...)
implicit class IC1[T <: IC1Filter](val v : MyPrettyClass[T]){...}
implicit class IC2[T <: IC2Filter](val v : MyPrettyClass[T]){...}
(...)

for example, if I have

class MC extends MyClass[IC1Filter with IC3Filter with IC4Filter with Any]

MC must be parsed by implicit classes IC1 or IC3 or IC4 but not by IC2 or IC5

--

I use HList composed of "Filters" in order to dynamically define the T parameter. When the wanted HList is created, I require to agregate the HList component in order to produce parsable filters for the implicit classes

So I need for example :

 IC1FIlter :: IC3Filter :: IC4Filter :: HNil

to be transformed to

 IC1Filter with IC3Filter with IC4Filter with Any

来源:https://stackoverflow.com/questions/19664593/type-level-shapeless-aggregating-a-hlist-type-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!