scala - 是什么导致匹配元组列表的模式保护中出现这种类型错误
问题描述
我有以下代码:
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
。
推荐阅读
- javascript - 在推入数组之前检查 Obj Id 值
- loops - lua表错误的循环顺序
- git - 如何创建一个makefile来共享githooks
- python - 输入多个打印函数以输出姓名牌时如何避免语法错误
- tomcat - 使用 curl 部署的 jenkins tomcat 不会更新战争文件
- anaconda - 如何组合“conda create”参数“--file”、“--prefix”和“--copy”?
- amazon-web-services - 无法将文件复制到 S3
- android - 如果在 Android 10+ 中禁用旧版外部存储访问,则以编程方式测试
- python - 我如何抓取这个网站以便候选人按列表分类?
- arrays - 合并排序如何在长度为 N 的数组上工作?