On Dec 3, 2:08 pm, Ian Lance Taylor <
i...@google.com> wrote:
Yes, that's a fair question. But if the compiler is willing to spend a
little extra little cycle to figure out the type, what makes up the
reason for keeping "var" there too? Does "var" give the type info? Or
does it give anything to help figuring out anything?
I think technically, a = 1, or a = any_primitive (type can be
determined in compile time) does not really need either type or var
(if we are willing to let compiler do the best work). You've got
const, func, type, etc, the only thing left is variables. There is
impossible to make a such statement ambiguity to parse I believe. Am I
right? Then I think it goes back to your previous comment, its
probably for consistency and/or readability. Then I would probably
argue that "var a int = 1" actually adds more consistency and
readability. For consistency, people would not able to arbitrarily
pick var a = 1 or a := 1 by their own preferences (though in file
scope, only var a = 1 is allowed but that's just because the current
law made so, there is no logical reason one can't do a := 1 in top
level) and apparently in functions, we can use either. For
readability, one does not have to rely on parsing someone else's
different coding preference in order to understand the code but rather
conforming to a strict grammar law.
There is also a little bit of rise by doing var a = 1 style I think.
Because compiler can only link the most popular type to this
identifier, it may not be the actual one that the author really wants.
What if the author wants uint32 instead of int? This is actually
severer in Go because different primitive types are not
interchangeable. int, int32, uint32, etc are just complete different
types. It might be more vulnerable to get buffer overflowed.
>
> Ian