In shapeless, have two lists such that one contains typeclasses of the other

拈花ヽ惹草 提交于 2019-12-03 12:45:47

Mapped provides precisely this constraint without the additional need for Length. From the documentation:

Type class witnessing that the result of wrapping each element of HList L in type constructor F is Out.

Here's how it looks in 1.2.4:

import shapeless._

def foo[L1 <: HList, L2 <: HList](l1: L1, l2: L2)(implicit
  ev: MappedAux[L1, Ordering, L2]
) = ()

val l1 = 1 :: 1.2 :: "hello" :: HNil
val l2 = Ordering[Int] :: Ordering[Double] :: Ordering[String] :: HNil
val l3 = Ordering[Int] :: Ordering[Double] :: Ordering[Char] :: HNil

And then:

scala> foo(l1, l2)

scala> foo(l1, l3)
<console>:17: error: could not find implicit value for parameter ev: ...

As expected. For 2.0 just add a shapeless.ops.hlist._ import and replace MappedAux with Mapped.Aux and you're ready to go.

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