【问题标题】:Using an HLists as an arguments to a function [duplicate]使用 HLists 作为函数的参数 [重复]
【发布时间】:2013-12-04 15:16:18
【问题描述】:

是否可以将 HList 作为函数参数传递?

这是我目前所拥有的:

import shapeless._
import poly._

object id extends (Id ~> Id) {
  def apply[T](f: shapeless.Id[T]): shapeless.Id[T] = f
}

object ListHandler {
  def mapOverHlist[ListType <: HList](list: ListType) = list.map(id)
}

val x = "foo" :: "bar" :: 0xb33f :: HNil    
ListHandler.mapOverHlist(x)

导致此错误:

could not find implicit value for parameter mapper:   
shapeless.ops.hlist.Mapper[ShapelessSpec.this.id.type,ListType]

【问题讨论】:

    标签: scala shapeless


    【解决方案1】:

    错误文本包含解决方法:

    import shapeless.ops.hlist.Mapper
    
    def mapOverHlist[L <: HList](list: L)(implicit m: Mapper[id.type, L]): m.Out =
      list.map(id)
    

    方法map 接受一个隐式参数,你应该提供这样的参数(通过在你的方法声明中添加相同的参数)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-09
      • 2018-01-24
      • 1970-01-01
      • 1970-01-01
      • 2014-12-23
      • 2017-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多