将其解释为您尝试使用 2 个参数(而不是一个元组参数)调用 += 方法。
请尝试以下方法之一
x += ((1, 2))
val t = (1, 2)
x += t
x += 1 -> 2 //syntactic sugar for new tuple
请注意,由于有多个 += 重载,编译器无法确定您正在尝试使用单个元组参数调用该方法:
def +=(elem : A) : Growable.this.type
def +=(elem1 : A, elem2 : A, elems : A*) : Growable.this.type
这里没有第二次重载,编译器可以解决。下面的例子可以说明:
class Foo {
def bar(elem: (Int, Int)) = ()
def baz(elem: (Int, Int)) = ()
def baz(elem1: (Int, Int), elem2: (Int, Int)) = ()
}
以您调用 += 的方式调用 bar 现在可以正常工作,但会出现警告:
foo.bar(2, 3) //No confounding overloads, so the compiler can figure out that we meant to use a tuple here
warning: Adapting argument list by creating a 2-tuple: this may not be what you want.
以您调用+= 的方式调用baz 失败,并出现类似错误:
f.baz(3, 4)
error: type mismatch;