【发布时间】:2017-07-31 01:03:20
【问题描述】:
我想知道,为什么这不起作用:
import scala.collection.mutable
var array: mutable.Seq[Int] = mutable.ArrayBuffer[Int]()
array += 5
我收到一条错误消息,指出 += 仅适用于字符串,这是为什么呢?
error: value += is not a member of scala.collection.mutable.Seq[Int]
Expression does not convert to assignment because:
type mismatch;
found : Int(5)
required: String
expansion: array = array.$plus(5)
array += 5
^
【问题讨论】:
-
同时使用
var和可变集合是冲突的。最好将可变集合声明为val。 -
@cchantep 如果我这样做,我将无法改变集合...
"Expression does not convert to assignment because receiver is not assignable" -
看看doc。无论如何,将
var与可变类型一起使用是没有意义的