为什么Scala说类型不匹配是一个好的定义?

为什么Scala说类型不匹配是一个好的定义?,scala,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?? 有以下错误: error: type mismatch;

代码片段

  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??
有以下错误:

error: type mismatch;
found   : Seq[(String, Any)]
required: Seq[(String, Integer)]


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

Int
不是
整数。您有
行:Seq[(String,Integer)]
,但您正试图向其中添加
(“etc”,fileCount):(String,Int)
。这个错误消息有点奇怪,我会告诉你的<代码>整数
几乎不应该出现在代码中;您应该将其替换为
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)

提示:
scala.Int
java.lang.Integer
的最低共同祖先是什么?在scala 2.13中,
行:+=(“etc”,“2.toInt)
编译并运行时没有错误。@jrook。我认为OP使用的是2.12或更高版本,因为它确实表现出这种行为。我不完全清楚为什么会发生变化,特别是因为
Int
Integer
显然仍然不同(请尝试
隐式[Int=:=Integer]
)。可能正在使用一些黑魔法。谢谢。是的,我正在使用Scala 2.11.8版(经过编辑的问题)