Scala 中 a += b 什么时候变成 a = a + b ?
When does a += b become a = a + b in Scala?
仅限 Scala 有时 脱糖
a += b
到
a = a + b
但并非总是如此。例如,一些可变集合定义了一个 +=
方法,而它变成了
a.+=(b)
这是行为吗
- 完全取决于
a
上是否有合适的+=
方法? (包括这种行为还有其他例子吗?)
- 与对象是 val 还是 var 无关?
相关例子
改编自 Scala 编程,在
var s = Set("a", "b")
s += "c"
In this case, the second line of code s += "c"
is essentially shorthand for:
s = s + "c"
When does a += b become a = a + b in Scala?
当没有适用的+=
方法时,是适用的+
方法,a
是可赋值的(即是var
或 a_=
方法)。
或者如 the spec 所说:
The re-interpretation occurs if the following two conditions are fulfilled.
- The left-hand-side
l
does not have a member named +=
, and also cannot be converted by an implicit conversion to a value with a member named +=
.
- The assignment
l = l + r
is type-correct. In particular this implies that l
refers to a variable or object that can be assigned to, and that is convertible to a value with a member named +
.
Is this behaviour
- entirely determined by whether there is a suitable += method on a?
- independent of whether the objects are val or var?
不完全是。如果有合适的 +=
方法,它将被调用,而不管任何其他因素(例如 a
是可分配的)。但如果没有,其他因素将决定它是否已脱糖或收到错误消息。
请注意,您收到的错误消息与您从脱糖版本中收到的错误消息不同:当脱糖标准不适用时,您会收到一条错误消息,告诉您“+= is not ... 的成员”,加上无法应用脱糖的解释(例如“接收器不可分配”或如果 a + b
会产生您从 a + b
得到的类型错误类型错误)。
第一次回答有点仓促,抱歉。经过一些研究并更好地阅读了 @sepp2k 的回答和评论后,我得出结论,Scala 中的一些 类 实现了 +=
方法,而其他只是 +
方法,我玩了一些 Scala 代码,例如:
//Set, Int, Double, String implements the "+" method,
//and then "+=" is syntactic sugar of a = a + b
var set = Set("a", "b")
set += "c"
var num = 3
num += 2
var str = "43"
str += 5
var l = List()
l += "someString"
// As you mention, MutableList implements "+=" method, and when you do
// mutL += 4, is the same as call the method mutL.+=
var mutL = new mutable.MutableList[Int]
mutL += 4
仅限 Scala 有时 脱糖
a += b
到
a = a + b
但并非总是如此。例如,一些可变集合定义了一个 +=
方法,而它变成了
a.+=(b)
这是行为吗
- 完全取决于
a
上是否有合适的+=
方法? (包括这种行为还有其他例子吗?) - 与对象是 val 还是 var 无关?
相关例子
改编自 Scala 编程,在
var s = Set("a", "b")
s += "c"
In this case, the second line of code
s += "c"
is essentially shorthand for:
s = s + "c"
When does a += b become a = a + b in Scala?
当没有适用的+=
方法时,是适用的+
方法,a
是可赋值的(即是var
或 a_=
方法)。
或者如 the spec 所说:
The re-interpretation occurs if the following two conditions are fulfilled.
- The left-hand-side
l
does not have a member named+=
, and also cannot be converted by an implicit conversion to a value with a member named+=
.- The assignment
l = l + r
is type-correct. In particular this implies thatl
refers to a variable or object that can be assigned to, and that is convertible to a value with a member named+
.
Is this behaviour
- entirely determined by whether there is a suitable += method on a?
- independent of whether the objects are val or var?
不完全是。如果有合适的 +=
方法,它将被调用,而不管任何其他因素(例如 a
是可分配的)。但如果没有,其他因素将决定它是否已脱糖或收到错误消息。
请注意,您收到的错误消息与您从脱糖版本中收到的错误消息不同:当脱糖标准不适用时,您会收到一条错误消息,告诉您“+= is not ... 的成员”,加上无法应用脱糖的解释(例如“接收器不可分配”或如果 a + b
会产生您从 a + b
得到的类型错误类型错误)。
第一次回答有点仓促,抱歉。经过一些研究并更好地阅读了 @sepp2k 的回答和评论后,我得出结论,Scala 中的一些 类 实现了 +=
方法,而其他只是 +
方法,我玩了一些 Scala 代码,例如:
//Set, Int, Double, String implements the "+" method,
//and then "+=" is syntactic sugar of a = a + b
var set = Set("a", "b")
set += "c"
var num = 3
num += 2
var str = "43"
str += 5
var l = List()
l += "someString"
// As you mention, MutableList implements "+=" method, and when you do
// mutL += 4, is the same as call the method mutL.+=
var mutL = new mutable.MutableList[Int]
mutL += 4