gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:45:03 25 4
gpt4 key购买 nike

我有以下代码:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com