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

标签 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

最佳答案

在此处删除[Int]

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

应该只是
def splitSeq(in: List[Int], ...

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

关于scala - 是什么在模式保护中导致匹配元组列表的类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54443888/

相关文章:

c - C中LinkedList遍历列表时出错

scala - 无法在 Scala.IO.Source 中使用相对路径

scala - 向 Spark 提交任务

Scala:可以直接引用本身吗?

java - 线程 "main"java.util.InputMismatchException 使用 Double 时出现异常

java - Lambda 表达式 "x cannot be resolved to variable"错误

scala - 为什么我的对象不是软件包<root>的成员,如果它在单独的源文件中?

Scalatest:组合断言

Java JUnit : The method X is ambiguous for type Y

c# - 当你的算法已经处理它时处理 "not all code paths return a value"的技术