为什么Scala说类型匹配不正确?

Pet*_*uss 0 scala

代码片段

  var line = Seq.empty[(String,Integer)]
  var fileCount : Int = -1   // good definition
  if (etc) 
      fileCount = tContSmry.getFileCount().toInt  // good cast
  line :+= ("etc", fileCount)  // where the error??
Run Code Online (Sandbox Code Playgroud)

有这个错误:

error: type mismatch;
found   : Seq[(String, Any)]
required: Seq[(String, Integer)]
Run Code Online (Sandbox Code Playgroud)

PS:使用Spark版本2.2.0.2.6.4.0-91,Scala版本2.11.8

HTN*_*TNW 7

Int不是Integer。您有line: Seq[(String, Integer)],但您正在尝试添加("etc", fileCount): (String, Int)它。错误消息有点奇怪,我给你。Integer应该几乎永远不会出现在您的代码中;您应该将其替换为Int

var line = Seq.empty[(String, Int)]
// side note: don't need a var here
val fileCount: Int = if(etc) tContSmry.getFileCount().toInt else -1
line :+= ("etc", fileCount)
Run Code Online (Sandbox Code Playgroud)