[英]Map for generic HList
Say we have following method 说我们有以下方法
def func[T <: HList](hlist: T, poly: Poly)
(implicit mapper : Mapper[poly.type, T]): Unit = {
hlist map poly
}
and custom Poly 和自定义聚
object f extends (Set ~>> String) {
def apply[T](s : Set[T]) = s.head.toString
}
So I can use this func
like 所以我可以像这样使用这个
func
func(Set(1, 2) :: Set(3, 4) :: HNil, f)
In my code I have small number of Polies and a big number of func
invocations. 在我的代码中,我只有少量的Polies和大量的
func
调用。 For this purpose I tried to move poly: Poly
to implicit parameters and got expected message 为此,我尝试将
poly: Poly
移至隐式参数并得到预期的消息
illegal dependent method type: parameter appears in the type of another parameter in the same section or an earlier one
How could I change or extend poly: Poly
parameter to avoid this error (I need to keep type signature func[T <: HList](...)
)? 如何更改或扩展
poly: Poly
参数以避免此错误(我需要保持类型签名func[T <: HList](...)
)?
Maybe you could use the "partially applied" trick using a class with an apply
method : 也许您可以使用带有
apply
方法的类来使用“部分应用”的技巧:
import shapeless._
import ops.hlist.Mapper
final class PartFunc[P <: Poly](val poly: P) {
def apply[L <: HList](l: L)(implicit mapper: Mapper[poly.type, L]): mapper.Out =
l map poly
}
def func[P <: Poly](poly: P) = new PartFunc(poly)
With your poly f
: 与您的poly
f
:
val ff = func(f)
ff(Set(1, 2) :: Set(3, 4) :: HNil) // 1 :: 3 :: HNil
ff(Set("a", "b") :: Set("c", "d") :: HNil) // a :: c :: HNil
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.