如何使用无形拆分scala列表

时间:2012-12-24 13:23:47

标签: scala shapeless

我正在尝试在N处拆分大小为S的列表,其中已知N,M总和为S.这不会编译:

def splitIt[N <: Nat,
            M <: Nat,
            S <: Nat](u: Sized[List[Int], N] {type A = N},
                      v: Sized[List[Int], M] {type A = M},
                      t: Sized[List[Int], S] {type A = S})(implicit sum: SumAux[N, M, S]): Unit = {
  val z = t.splitAt[N]
}

错误

No implicit view available from List[Int] => scala.collection.GenTraversableLike[S,List[Int]].

not enough arguments for method sizedOps: (implicit evidence$2: List[Int] => scala.collection.GenTraversableLike[S,List[Int]])shapeless.SizedOps[S,List[Int],S]. Unspecified value parameter evidence$2.

最终正确的版本

def splitIt[N <: Nat,
            M <: Nat, S <: Nat](u: Sized[List[Int], N] {type A = Int},
                                v: Sized[List[Int], M] {type A = Int},
                                t: Sized[List[Int], S] {type A = Int})(implicit sum: DiffAux[S, N, M], toInt: ToInt[N]): Unit = {
  val z = t.splitAt[N]
}

1 个答案:

答案 0 :(得分:5)

type A需要列表元素的类型,而不是size参数。这就是为什么它试图转换为GenTraversableLike[A, List[Int]]。您需要在每种情况下将A设置为Int