【问题标题】:Map for generic HList通用 HList 的映射
【发布时间】:2016-06-27 20:15:52
【问题描述】:

假设我们有以下方法

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)

在我的代码中,我有少量的 Polies 和大量的 func 调用。为此,我尝试将poly: Poly 移动到隐式参数并得到预期的消息

illegal dependent method type: parameter appears in the type of another parameter in the same section or an earlier one

如何更改或扩展poly: Poly 参数以避免此错误(我需要保留类型签名func[T &lt;: HList](...))?

【问题讨论】:

    标签: scala shapeless hlist


    【解决方案1】:

    也许您可以使用具有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)
    

    与你的聚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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-11
      • 2023-03-04
      • 2015-12-03
      • 2017-02-10
      • 1970-01-01
      • 1970-01-01
      • 2014-09-15
      • 1970-01-01
      相关资源
      最近更新 更多