FAQ

On 2/20/2013 8:43 AM, Norbert Roos wrote:
On 02/19/2013 08:20 AM, Johann Höchtl wrote:

[...] The variable "found" is hidden. This is clearly a mistake yet get's
unspotted by the current compiler. [...]
I would propose another radical solution for this: Simply that shadowing
variables is not allowed. When you are about to shadow a variable, use a
different variable name.
I tend to agree, but it does break some existing code, and there's
sort of an idiom of redeclaring "ok".

There are already some useful cases where shadowing is prohibited.
Imports of the "." import into the local namespace, and it's an error
to locally redefine a function that's also imported. Probably a good
decision, although Python users used to "monkey patching" probably won't
like it.

A more troublesome shadowing problem is this one:

http://play.golang.org/p/nS1aRrP78F

The multi variable form of ":=" is a mix of assignment and
declaration/initialization. You can't tell by looking which
variables are merely assigned and which are being created.
Does a variable in a multi variable ":=" get created even
if it shadows a variable in an outer scope?

The answer is yes. Context dependent declaration can cause
shadowing. That may be troublesome. The semantics of
multi-variable ":=" are new to Go, so there's little field
experience on how this ought to work.

It's an area to watch, because it leads to a class of
unchecked non-local bugs which can be introduced during code
maintenance. Those are the ones to worry about.

John Nagle

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

People

Translate

site design / logo © 2021 Grokbase