Scala代码生成奇怪的类型错误

时间:2019-03-19 23:40:29

标签: scala typing

  def foldLeft2[A,B](as: List[A], z: B)(f: (B, A) => B): B = as match {
    case Nil => z
    case x::xs => foldLeft2(xs, f(z, x))(f)
  }

  def foldSum2[Int](xs: List[Int]): Int = foldLeft2[Int, Int](xs, 0)((a: Int, b: Int) => a + b)

上面的代码会产生两个错误:

Error:(109, 67) type mismatch;
 found   : Int(0)
 required: Int
  def foldSum2[Int](xs: List[Int]): Int = foldLeft2[Int, Int](xs, 0)((a: Int, b: Int) => a + b)

Error:(109, 94) type mismatch;
 found   : Int
 required: String
  def foldSum2[Int](xs: List[Int]): Int = foldLeft2[Int, Int](xs, 0)((a: Int, b: Int) => a + b)

我不明白这两个错误消息。特别是考虑到我甚至从未在函数签名中指定字符串类型的事实。有人知道怎么了吗?

0 个答案:

没有答案