是什么在模式保护中导致匹配元组列表的类型错误

时间:2019-01-30 15:21:06

标签: scala compiler-errors

我有以下代码:

object foo{
  def splitSeq[Int](in: List[Int], out: List[(Int,Int)] = Nil): List[(Int,Int)] = (in,out) match {
    case (Nil,o) => o
    case (next :: rest, Nil) =>
      splitSeq(rest, List((next,next)))
    case (next :: rest, (start, end) :: accRest) if (end + 1 == next) =>
      splitSeq(rest, (start, next) :: accRest)
    case (next :: rest, acc) =>
      splitSeq(rest, (next,next) :: acc)
  }
}

它会产生以下编译器错误,我完全不理解:

~/tmp> scalac test.scala 
test.scala:6: error: type mismatch;
 found   : Int(1)
 required: String
  case (next :: rest, (start, end) :: accRest) if (end + 1 == next) =>
                                                         ^
one error found

1 个答案:

答案 0 :(得分:2)

在此处删除[Int]

def splitSeq[Int](in: List[Int], ...

应该是

def splitSeq(in: List[Int], ...

您声明了类型参数Int(就像您写了def splitSeq[T](in: List[T], ...),而不是使用标准的Int。并且此类型参数Int遮盖了标准Int