代码之家  ›  专栏  ›  技术社区  ›  ziggystar

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

  •  1
  • ziggystar  · 技术社区  · 7 年前

    我有以下代码:

    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 回复  |  直到 7 年前
        1
  •  2
  •   Dmytro Mitin    7 年前

    去除 [Int] 在这里

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

    应该只是

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

    您声明了类型参数 Int (就像你写的 def splitSeq[T](in: List[T], ... )而不是使用标准 int . 这个类型参数 int 阴影标准 int .