On Jun 11, 8:02 am, Steven D'Aprano <steve +comp.lang.pyt...@pearwood.info> wrote: > On Mon, 10 Jun 2013 19:36:44 -0700, rusi wrote: > > Pascal introduced the idea of block structure -- introduce a name at one > > level, override it at a lower level. [Ok ALgol introduced, Pascal > > popularized]. > > This has caused more trouble than it has solved. > > > And so languages nowadays tend to 'protect' against this feature. > > Apart from Erlang, got any other examples? Because it seems to me that in > languages with nested scopes or namespaces, shadowing higher levels is > exactly the right thing to do.
This is just opening up the definition of block-structure and saying its a good thing. > Certainly it would be a PITA, and defeat > the purpose of having nested scopes, if inner names had to be globally > unique. Wouldn't it be absolutely horrible if adding a global variable > "foo"[1] suddenly meant that all your functions that used "foo" as a > local variable stopped working? > > [1] For some value of "foo". Your opinion. Not so convincing if the sequence of composing the program was the other-way-round: if I have a global variable, say errno, and 'lose' it by introducing a local variable errno. And in fact for a reader of a program, the order of its writing should not matter. Which brings us pat into Terry's example. [Also notice that changing from a 'parametric-semantic' name like foo to a more 'fixed-semantic' name like 'errno' or 'list' changes the desirability of this feature. > > I take it you have never programmed in a programming language with a > single, flat, global namespace? :-) Well Ive used Basic and Assembler -- which are fun in the way that childhood and mountaineering respectively are fun. What it seems you are not getting about Erlang's outlook about block- structure is this: There are two separable aspects to it: 1. Names can be created in local scopes which dont leak into (more) global scopes -- a desirable feature 2. Names in local scopes can override names in global scope -- a dangerous feature [BTW which is what this thread is about]. And Erlang's approach seems to be the most nuanced -- you can do it if you go out of your way to say: "unstick the global namespace". This is somewhat analogous to gotos in Pascal. For Pascal goto was a sufficiently undesirable feature that using it was not completely easy. However if you did surely want it, you had to declare the goto label. Or by example: def foo(x)... def bar(x,y)... there is no reason to confuse the two xes. Whereas x = ... def foo(x)... Now there is! The first should be encouraged, the second discouraged. -- http://mail.python.org/mailman/listinfo/python-list