a:=3b:=7a=+ba+=+bfmt.Println("a", a, "b", b)
I believe the +b is extraneous and does not modify command operation, in both cases. Could go fmt simplify this by removing the plus operator? Similarly we remove things like braces and semicolons when they are unnecessary and do not affect program operation.
Running go tip.
The text was updated successfully, but these errors were encountered:
Could go fmt simplify this by removing the plus operator?
gofmt is not a tool that simplifies arithmetic expressions, though. It's a code formatter. Should gofmt also remove the addition in b = a + 0 ? What about a = b + c*0 ? This kind of things belong in a static checker, IMO.
Similarly we remove things like braces and semicolons
Braces and semicolons are syntactic tokens. Eliding an unnecessary token and simplify an arithmetic expression are two very different things.
While we do certain rewrites (and in fact just introduced new ones to canonicalize the formatting of number literals for Go 1.13), we don't want to change what a programmer intended to express in the code. But I admit I don't have a good example except that perhaps one wanted to emphasize a +x next to a -x on separate lines.
You are correct that removing a unary + will not affect the expression's value (explicitly stated by the spec for complex and floating-point numbers); for ints +x is the same as 0+x which is x in all cases; for strings a unary + is not permitted).
In fact, one could remove the unary + from the language - it's never needed and only added for symmetry; which is why we probably don't want to remove it with a rewrite.
As an aside, note that we cannot easily simplify 0 + x without type information: If x is the floating-point value -0.0, than 0 + x results in 0.0 (not -0.0); that is 0 + x cannot be simplified w/o changing the program for floating-point or complex numbers.
I'm leaving this open for now for the wider proposal committee to make a decision, but I am leaning against this change.
changed the title
cmd/go: go fmt can remove plus operator before variableMar 7, 2019
Ironically, the floating-point value -0.0 is a case where gofmt arguably should remove the leading operator, since the language spec defines that constant to result in +0.0 instead of -0.0 as one might expect.