【问题标题】:Scala Type Mismatch Error on Map地图上的Scala类型不匹配错误
【发布时间】:2012-06-15 01:12:28
【问题描述】:

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

错误:类型不匹配;找到:List[Double](在方法中 计算HaarWaveletI)]

必需:列表[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....")
  }
}

【问题讨论】:

    标签: scala type-erasure


    【解决方案1】:

    calculateHaarWaveletI 的定义中的类型参数Double 正在遮蔽scala.Double。您可以完全删除类型参数,代码应该按预期工作。请参阅我的回答 here 了解更多详情。

    【讨论】:

    • 谢谢。我最初为所有方法添加了一个类型参数,然后将它们删除,这个不知何故逃脱了:)
    猜你喜欢
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多