地图上的Scala类型不匹配错误

时间:2012-06-14 16:56:56

标签: scala type-erasure

我在这里缺少什么? 这是我得到的错误......

  

错误:类型不匹配;发现:列出[双](方法中    calculateHaarWaveletI)]

     

必需:列出[scala.Double]

     

Console.println(list2Tuples(ls.take(N)))

这是我的代码..

    object HaarWavelet {

  def calculateHaarWavelet(ls: List[Double]): List[Double] = {
    if (ls.length % 2 != 0) throw new RuntimeException("Need even number of elements to calculate HaarWavelet")
    calculateHaarWaveletI(ls, ls.length)
    def calculateHaarWaveletI[Double](ls: List[Double], n: Int): List[Double] = {
      Console.println(list2Tuples(ls.take(n)))
      null
    }
    null
  }

  def processTuple(x: (Double, Double)): (Double, Double) = {
    val f = (x._1 + x._2) / 2
    (f, x._1 - f)
  }

  def list2Tuples(ls: List[Double]): List[(Double, Double)] = {
    if (ls.isEmpty) return List()
    (ls.head, ls.tail) match {
      case (_, Nil) => List()
      case (x, y) => List((x, y.head)) ::: list2Tuples(y.tail)
    }
  }

  def main(args: Array[String]) {
    Console.println("Starting....")
    Console.println(calculateHaarWavelet(List(8.0, 4.0)))
    Console.println("Done....")
  }
}

1 个答案:

答案 0 :(得分:5)

Double定义中的类型参数calculateHaarWaveletI是影子scala.Double。您可以完全删除类型参数,代码应该按预期工作。有关详细信息,请参阅我的回答here