Does "Global" outside a function change anything? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
mikeyww
Posts: 26600
Joined: 09 Sep 2014, 18:38

Does "Global" outside a function change anything?

Post by mikeyww » 01 Jan 2023, 16:28

Imagine two scenarios.

1. I declare my variable as Global outside the function. Inside the function, I can access this variable without another Global declaration, or I can assign this global variable, but only with another Global declaration.

2. I do not declare my variable as Global outside the function. I just declare it as usual, like f := 5, outside the function. Inside the function, I can still access this variable as a global variable without another Global declaration, or I can assign this global variable, but only with a Global declaration.

Thus, unlike in v1, declaring a variable as Global outside a function seems to have no purpose, as it changes nothing about how the variable can be used as a global variable inside or outside the function. A coder who then uses Global outside a function might be confused or actually mistaken about what it does. Thus, why doesn't it generate a warning there?


swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Does "Global" outside a function change anything?

Post by swagfag » 02 Jan 2023, 05:19

The global keyword is currently redundant when used in global scope, but can be used for clarity.
not sure what a slightly differently syntaxhighlighted glorified comment knockoff is meant to clarify, considering all variables are global for reading and no variables are global for writing. i dont see the point in keeping around keywords that dont do anything

User avatar
mikeyww
Posts: 26600
Joined: 09 Sep 2014, 18:38

Re: Does "Global" outside a function change anything?

Post by mikeyww » 02 Jan 2023, 06:34

In general, I had the same thought. At least it seemed possible that someone who uses this declaration in other languages could be confused or mistaken by AHK's handling of it, and a warning may help. The declaration does have relevance inside a function, and I can see the idea behind it there. If #including a lot of different kinds of scripts, this could make a difference (i.e., prevent a bug). There is also the added convenience of not having to declare as global when reading but not writing variables.

lexikos
Posts: 9553
Joined: 30 Sep 2013, 04:07
Contact:

Re: Does "Global" outside a function change anything?

Post by lexikos » 02 Jan 2023, 23:28

Global declarations are permitted outside of functions for several reasons.
  • A declaration shows intent that the variable will be used elsewhere in the script (in contrast with, for example, a variable used only within a small portion of the code, that would have been better off inside a function).
  • Declarations can be used in a prominent place to show which global variables are expected to exist, even if they are assigned in various other parts of the file.
  • Tools that use static analysis can differentiate between a declared variable and an undeclared variable. There used to be a #MustDeclare directive, which at the time, required a new keyword for declaring a non-super global variable. Permitting global declarations outside functions enables tools to reproduce the functionality of #MustDeclare without requiring any unique syntax.
  • For a global variable to be assigned dynamically, it must either be declared somewhere or referenced non-dynamically outside a function. An undeclared reference inside a function would not resolve to a global unless some other non-dynamic reference caused it to be created.
  • A declaration without an initializer acts as a non-dynamic reference to the variable without producing an executable line. By contrast, (var) is executable and therefore subject to #Warn Unreachable.

@swagfag
Your reference to "glorified comment" indicates that you already understood that it does indeed provide clarity, if only as much as a comment. Even if the benefit was only for humans reading the script, why require the user to write (x) ; global or x := unset ; global when we already have a keyword for declaring global variables?

mikeyww wrote:At least it seemed possible that someone who uses this declaration in other languages could be confused or mistaken by AHK's handling of it
It is generally the case that someone who is familiar with one behaviour could be confused by some other behaviour. However, this works both ways: if the current behaviour matches other popular languages, it may be less likely to cause confusion.

Aside from AutoHotkey v1, I am not aware of any languages where a "global" declaration in global scope is permitted but not mandatory and affects the resolution of variables inside functions/nested scopes.

It is more likely that someone who uses other languages (or AutoHotkey!) could be confused by:
  1. The v1 behaviour that undeclared "global" variables are not visible inside functions.
  2. The v2 behaviour that assigning to an undeclared variable inside a function implies it will be local, never global.
I spent quite a while researching before I changed the rules of scope in v2. As far as I can recall, PHP and Python were the only languages I found where variable references inside functions default to local even when a global variable exists (except in Python when the variable is only being read). Both of these languages permit global variables to be declared outside of functions, without such declarations having any effect on code inside functions.

In PHP, the variable reference inside the function would still be local/undefined.

Code: Select all

<?php
global $a;
$a = 42;
function f() {
	echo $a; // Warning: Undefined variable $a
}
f();
In Python, the variable reference inside the function would be local if it is being assigned, otherwise global, regardless of whether it is declared outside the function. In other words, it appears to be the same as AutoHotkey v2.

Code: Select all

>>> global declared
>>> declared = 1
>>> undeclared = 2
>>> def read_declared():
...     return declared
...
>>> def read_undeclared():
...     return undeclared
...
>>> def write_declared():
...     declared = 3
...
>>> def write_undeclared():
...     undeclared = 4
...
>>> read_declared()
1
>>> read_undeclared()
2
>>> write_declared()
>>> declared
1
>>> write_undeclared()
>>> undeclared
2

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Does "Global" outside a function change anything?

Post by swagfag » 29 Jan 2023, 08:46

the does-nothing-global sure helped here viewtopic.php?p=504560 /s

iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

Re: Does "Global" outside a function change anything?

Post by iseahound » 29 Jan 2023, 09:26

I think it's meant for a certain paradigm, perhaps a company-wide script with multiple authors, where all global variables are declared at the top for simplicity. I would probably switch over to explicitly writing global when the number of actual global variables exceeds 20 perhaps.

Post Reply

Return to “Ask for Help (v2)”