通用 HList 的映射 [英] Map for generic HList
本文介绍了通用 HList 的映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我们有以下方法
def func[T <: HList](hlist: T, poly: Poly)
(implicit mapper : Mapper[poly.type, T]): Unit = {
hlist map poly
}
和自定义多边形
object f extends (Set ~>> String) {
def apply[T](s : Set[T]) = s.head.toString
}
所以我可以像这样使用func
func(Set(1, 2) :: Set(3, 4) :: HNil, f)
在我的代码中,我有少量的策略和大量的 func
调用.为此,我尝试将 poly: Poly
移动到隐式参数并得到预期的消息
In my code I have small number of Polies and a big number of func
invocations. For this purpose I tried to move poly: Poly
to implicit parameters and got expected message
illegal dependent method type: parameter appears in the type of another parameter in the same section or an earlier one
我如何更改或扩展 poly: Poly
参数以避免此错误(我需要保留类型签名 func[T <: HList](...)
)?
How could I change or extend poly: Poly
parameter to avoid this error (I need to keep type signature func[T <: HList](...)
)?
推荐答案
也许您可以使用带有 apply
方法的类来使用部分应用"技巧:
Maybe you could use the "partially applied" trick using a class with an apply
method :
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)
用你的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
这篇关于通用 HList 的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文