Page 1 of 1

Variable scope - remove the need for re-declaring globals?

Posted: 29 Oct 2019, 05:14
by sirksel
[Moderator's note: Topic split from Changes to %fn%(), fn.call() or func('fn') syntax?. Title changed.]

What was the reasoning for keeping super-globals in the first place? Put another way, why isn't the top-level scope just another enclosing scope, below which all enclosing-scope locals are visible but can be shadowed? Is it just legacy of the language or is there something else special about super-globals?

Code: Select all

global glo := 'super'
loc := 'cool'
msgbox(glo loc)  ;'supercool'
fun() {
  msgbox(glo loc)  ;'super', not 'supercool'
  outer := 'way'
  innerfun() {
    inner := 'cool'
    msgbox(outer inner)  ;'waycool'
  }
  innerfun()
}
fun()

Re: Changes to %fn%(), fn.call() or func('fn') syntax?

Posted: 29 Oct 2019, 08:04
by kczx3
sirksel wrote:
29 Oct 2019, 05:14
What was the reasoning for keeping super-globals in the first place? Put another way, why isn't the top-level scope just another enclosing scope, below which all enclosing-scope locals are visible but can be shadowed? Is it just legacy of the language or is there something else special about super-globals?
That's how JavaScript works basically.

Re: Variable scope - remove the need for re-declaring globals?

Posted: 30 Oct 2019, 17:09
by lexikos
@sirksel That's a confusing question. If we make all non-local variables automatically accessible in all functions by default, thereby making the top-level scope "just another scope" and removing the need to distinguish between "global variables" and "super-global variables", is that removing super-globals or keeping them? All global variables would be super-global, but we would not be using that term anymore.

It is not super-globals that are special, but non-super globals. Super-global and local variables do not need to be declared in each enclosing scope; they are automatically accessible, like functions and labels. Only non-super globals require re-declaration inside a function (unless you use assume-global).

Similarly (and maybe not a coincidence), variable references inside PHP functions are local by default, and the global keyword must be used to make global variables accessible.
You may notice that this is a little bit different from the C language in that global variables in C are automatically available to functions unless specifically overridden by a local definition. This can cause some problems in that people may inadvertently change a global variable. In PHP global variables must be declared global inside a function if they are going to be used in that function.
Source: PHP: Variable scope - Manual
Like AutoHotkey, PHP's built-in variables are always available in all scopes. PHP describes them as "superglobal", but doesn't offer a way to create more of them.

Notice that the explanation in the PHP manual refers only to changing a global variable. Python solves that in a different way:
In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a value anywhere within the function’s body, it’s assumed to be a local unless explicitly declared as global.

Though a bit surprising at first, a moment’s consideration explains this. On one hand, requiring global for assigned variables provides a bar against unintended side-effects. On the other hand, if global was required for all global references, you’d be using global all the time. You’d have to declare as global every reference to a built-in function or to a component of an imported module. This clutter would defeat the usefulness of the global declaration for identifying side-effects.
Source: Programming FAQ — Python 3.8.0 documentation
Without an assignment, having a function access a global variable when an empty local variable was expected doesn't seem like much of an issue, because it implies that the variable is being used without being initialized. However, 1) explicit initialization is optional, and 2) a variable can be initialized by passing it to a function ByRef.

Some resources:
lua-users wiki: Local By Default (contains various quotes)
Why not "local by default".