Hi.
I realize that this is by design, as per the language specification.
a := 1
a, b := 3, 3 // "a" is redeclared and "b" is declared.
I find this somewhat troubling. I see that it saves a line of code (declaring b), but it also creates a chance of an unintentional redeclaration going unnoticed. It also might result in the resulting type (after the redeclaration) not being what you expected if the first line is changed to: a := 0.1
I was wondering what the logic was behind this decision? Am I missing something?
Thanks,
John
John Souvestre - New Orleans LA
Hi Harmen.
I agree, it is handy. But if “err” is the predominant reason for it, couldn’t you solve it by declaring “err” once, first?
John
John Souvestre - New Orleans LA
var m Missilevar err Error
m, err = loadedMissile()if err != nil {
// ... &c.}var eta time.Time
eta, err = launchMissile(m)if err != nil {// ... &c.}
Hi Harmen.
The first time you could use the “:=”, since you are declaring both. This avoids adding the extra 2 lines. So overall you would only need the 1 extra line in the seconds call.
I guess my concern is with a declaration statement which sometimes doesn’t declare.
Is the logic that the sometimes convenience outweighs the risk?
This is also something I believe the Golang caretakers wish they would have done differently, but it's done at this point and you just need to learn its behavior.
Now the 'ignored redeclaration' silently becomes a new declaration and will have the unintended consequence of losing the "modified" value at the end of the block.
Hi Benjamin.
The non-deterministic behavior of the statement, which depends on prior code located elsewhere. It also varies, depending on what scope the other code is in. The resulting error shows up later, not necessarily where the statement is located. In short, it complicates testing, troubleshooting, refactoring, and maintenance.
And the problem could be intermittent, data dependent. For example, imagine that the loop control variable you are using is actually a float type instead of an integer type as a result of this issued?
John
John Souvestre - New Orleans LA
From: golan...@googlegroups.com [mailto:golan...@googlegroups.com] On Behalf Of Benjamin Measures
Sent: 2014 August 18, Mon 09:58
To: golan...@googlegroups.com
Cc: 0xj...@gmail.com; dan.ko...@adelaide.edu.au
Subject: Re: [go-nuts] Short Variable Redeclaration
On Monday, 18 August 2014 15:38:53 UTC+1, johns wrote:
--
Hello Gary.
The Short Variable Redeclaration, when used as I presented it, isn’t type inferring for “a”. It’s using a previously declared “a” and the type assigned then.
To improve the wording of the question: Why use a declaration syntax for just assignment and not declaration?
John
John Souvestre - New Orleans LA
--
Hi Benjamin.
I’m inclined to think that calling this behavior “redeclaration” is misleading and/or confusing. The previously-declared variable is used, as it was declared then.
I think that perhaps “skipped-declaration” is a better description of what takes place.
a := 1
a, b := a+1, 3
println(a, b) // prints “2 3”
I believe that this shows that the previous value for “a” is used in its “re/skipped-declaration”.
John
John Souvestre - New Orleans LA
From: golan...@googlegroups.com [mailto:golan...@googlegroups.com] On Behalf Of Benjamin Measures
Sent: 2014 August 18, Mon 12:05
To: golan...@googlegroups.com
Cc: 0xj...@gmail.com; dan.ko...@adelaide.edu.au
Subject: Re: [go-nuts] Short Variable Redeclaration
--
Not so, the spec specifically addresses this:Unlike regular variable declarations, a short variable declaration may redeclare variables provided they were originally declared earlier in the same block
I did find it discussed in Issue 377. I was unable to find any consensus about changing it in Go 2.x, however.
On 19 Aug 2014 11:09, "John Souvestre" <jo...@sstar.com> wrote:
>
> Yes, I believe that our definitions of declare (and redeclare) differ. I associate declaring a variable with initializing it, and that isn't taking place with this type of "redeclare".
>
In C a declaration doesn't initialise.
In Go the 'var' keyword will declare and initialise variables.
The short declaration will declare the variables and then assign the given values to them. Since you're assigning values to them immediately it makes no difference whether they are initialised to anything before the assignment.
Hi Jesse.
Since we are speaking about Go, I’m using the Go Programming Language Specification as reference, not C. The spec says that a short variable declaration “is shorthand for a regular variable declaration with initializer expressions but no types”. It goes on to say “Redeclaration does not introduce a new variable; it just assigns a new value to the original.”
Ø Since you're assigning values to them immediately it makes no difference whether they are initialised to anything before the assignment.
It does makes a difference. The type of the old variable defines the type of both of the variables, and it must be compatible with the initialization expression for the new variable.
As proof, I repeat the example I sent previously:
a := 0.1
b := 1
a, b, c := 2, b + 3, 4
println(a, b, c) // prints "+2.000000e+000 4 4"
As you can see, “a” ends up a float64 rather than an int, as it would if “a” weren’t declared previously.
From the language spec: “A variable declaration creates a variable, binds an identifier to it and gives it a type and optionally an initial value.” A Short Variable Redeclaration does not create a variable, bind an identifier to it, give it a type, or allow the initial value to be optional. It does not even come close to meeting the definition of variable declaration. Thus my objection to the spec saying that it “redeclares” a variable.
I think that “skipped-declaration” or perhaps “reinitialized” is much more accurate, thus less confusing. And I think that this clarity is important given the subtle, possibly run-time, failure modes that this feature allows.
"A variable declaration creates a variable, binds an identifier to it and gives it a type and optionally an initial value."
"Unlike regular variable declarations, a short variable declaration may redeclare variables provided they were originally declared earlier in the same block with the same type, and at least one of the non-blank variables is new. As a consequence, redeclaration can only appear in a multi-variable short declaration. Redeclaration does not introduce a new variable; it just assigns a new value to the original." (emphasis mine)