++是+= 1的语法糖吗?

huangapple go评论72阅读模式
英文:

Is ++ syntactic sugar for += 1?

问题

据我理解,对地图进行索引会返回地图值的副本。考虑以下代码:

package main

import "fmt"

func main() {
    m := map[string]int{"xxx": 100}
    m["xxx"]++
    fmt.Println(m["xxx"]) // 输出 101
}

上面的代码输出 101,而我预期的是 100
我的推理是,m["xxx"] 返回分配给键 "xxx" 的值的副本(即 100),而操作符 ++ 会增加这个值的副本,但不会影响存储在地图中的原始值 - 只有副本发生变化。

然而,考虑到我们通过将键放在方括号中并使用 = 指定值(即 m[key] = value)来为地图键赋值。那么,如果将 m["xxx"]++ 转换为 m["xxx"] += 1,这相当于 m["xxx"] = m["xxx"] + 1,这就解释了上面代码的结果。

我的问题是递增运算符(++)是否是对递增赋值(+= 1)的语法糖。否则,我漏掉了什么?

英文:

As far as I understand, indexing a map returns a copy of the map value. With this in mind, consider the following code:

package main

import "fmt"

func main() {
	m := map[string]int{"xxx": 100}
	m["xxx"]++
	fmt.Println(m["xxx"]) // prints 101
}

The code above prints 101, whereas I expected 100.
My reasoning is that m["xxx"] returns a copy of the value assigned to the key "xxx" (i.e., 100), and the operator ++ increments this copy of the value but this doesn't affect the original value stored in the map – only the copy changes.

However, considering that we assign a value to a map key by putting the key within brackets and using = to specify the value (i.e., m[key] = value). Then, if m["xxx"]++ were translated to m["xxx"] += 1 by the compiler – which is, in turn, equivalent to m["xxx"] = m["xxx"] + 1 – this would explain the result of the code above.

My question is whether the increment operator (++) is syntactic sugar for the by-one addition assignment (+= 1). Otherwise, what am I missing?

答案1

得分: 2

我的推理是,m["xxx"]返回的是值的副本。我认为这个假设不公平。在index expressions的规范中只是简单地说明了以下内容:

  • 形式为a[x]的主表达式表示映射a中由x索引的元素。
  • 如果映射包含键为x的条目,a[x]是具有键x的映射元素

动词"表示"和"是"并不明确表示副本。只有在将索引表达式的结果赋值给变量时才会创建副本。

映射索引表达式本身是不可寻址的,因此无法对映射中存储的值进行内存别名。

至于递增运算符++是否是+= 1的语法糖,规范明确说明了这一点:

  • 以下赋值语句在语义上是等价的:
IncDec statement    Assignment
x++                 x += 1
x--                 x -= 1

因此,op++op += 1具有相同的含义,a[x]++会递增表示/是具有键x的映射元素的操作数a[x]

英文:

"My reasoning is that m["xxx"] returns a copy of the value" — I don't think this is a fair assumption. The specs under index expressions only say the following:

> A primary expression of the form a[x] denotes the element of the [...] map a indexed by x

and

> if the map contains an entry with key x, a[x] is the map element with key x

The verbs "denotes" and "is" arguably do not imply a copy. A copy is made only when you assign the result of the index expression to a variable.

The map index expression is simply not addressable, so that you can't memory alias the values stored in the map.

As for whether the increment operator ++ is syntactic sugar for += 1 or not, the specs explicitly state that:

> The following assignment statements are semantically equivalent:
<pre>
IncDec statement Assignment
x++ x += 1
x-- x -= 1
</pre>

So op++ has the same meaning of op += 1, and a[x]++ increments the operand a[x] which "denotes/is" the map element with key x.

答案2

得分: 1

语言规范指出,++/--运算符应该与地图索引表达式一起使用作为操作数。

https://go.dev/ref/spec#IncDec_statements

> "++"和"--"语句通过未类型化常量1递增或递减它们的操作数。与赋值一样,操作数必须是可寻址的或地图索引表达式

如何实现这个功能没有具体说明,因此在不同的编译器或同一编译器的不同版本中可能会有所不同。


如果你搜索你的Go安装中的cmd/compile/internal目录,这是Go编译器的源代码,如果我没记错的话,你会找到一些行似乎证实了你的猜测。然而,我对代码了解得远远不够,无法给出最终的答案。

示例:onetwo

gccgo实现的规范,我从未查看过,可能会或可能不会以相同的方式执行IncDec操作。

英文:

The language specification says that the ++/-- operators should work with a map index expression as the operand.

https://go.dev/ref/spec#IncDec_statements

> The "++" and "--" statements increment or decrement their operands by
> the untyped constant 1. As with an assignment, the operand must be
> addressable or a map index expression.

How that functionality should be implemented is not specified and may therefore be different across different compilers or different versions of the same compiler.


If you search the cmd/compile/internal directory of your Go installation, which is the source code of the Go compiler if I'm not mistaken, you'll find a number of lines that seem to confirm your guess. I have nowhere near enough knowledge about the code to provide a conclusive answer however.

Examples: one and two

And the gccgo implementation of the spec, which I've never looked at, may or may not do the IncDec the same way.

huangapple
  • 本文由 发表于 2022年2月18日 03:27:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/71164283.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定