我对无形特征概述中显示的示例感到困惑.
object size extends Poly1 {
implicit def caseInt = at[Int](x => 1)
implicit def caseString = at[String](_.length)
implicit def caseTuple[T, U]
(implicit st : Case.Aux[T, Int], su : Case.Aux[U, Int]) =
at[(T, U)](t => size(t._1)+size(t._2))
}
scala> size(((23, "foo"), 13))
res7: Int = 5
Run Code Online (Sandbox Code Playgroud)
提前谢谢了
我构建了以下内容:
import shapeless._
import poly._
object Main {
def main(args: Array[String]) = {
object iterateOverHList extends (List ~> Iterator) {
def apply[T](it: List[T]) = it.iterator
}
val x = List(1,2,3) :: List("cat","dog") :: HNil
val xIt = x map iterateOverHList
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码很棒,非常棒.但是,我还想要更多.我想,而不是指定我的HList将包含列表,允许任何Iterable.像这样:
import shapeless._
import poly._
object Main {
def main(args: Array[String]) = {
object iterateOverHList extends (Iterable ~> Iterator) {
def apply[T](it: Iterable[T]) = it.iterator
}
val x = List(1,2,3) :: List("cat","dog") :: HNil
val xIt = x …
Run Code Online (Sandbox Code Playgroud)