AHK V2: Why can I access outer function local variable in inner function? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
SAbboushi
Posts: 252
Joined: 08 Dec 2014, 22:13

AHK V2: Why can I access outer function local variable in inner function?

28 Jun 2018, 01:32

Documentation says:
•If the function is defined inside another function, the outer function's local and static variables cannot be accessed.
I'm finding this to not be the case, even if the outer function variable is static or the outer function is in Force-local mode. e.g.

Code: Select all

a:= sub1()

sub1()
{
	x:={1:12}
	x1:=2
	
	z:=sub2()

	sub2()
	{
		x.Delete(1) ; deletes item 1 in sub1()'s x array
		y:=3
	}
}

I find that in sub2(), x is a local variable with a reference to sub1()'s x array. Interestingly, if I remove the x.Delete(1) statement from sub2(), SciTe4AutoHotkey's Debugger Variable List no longer shows x as a local variable in sub2().

Any thoughts?
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: AHK V2: Why can I access outer function local variable in inner function?

28 Jun 2018, 04:28

Could you show where it says that in the help file because all quotes I can find are:
By default, a nested function may access non-dynamic local and static variables of any function which encloses it
A closure is a nested function bound to a set of free variables. Free variables are local variables of the outer function which are also used by nested functions.
Recommends AHK Studio
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: AHK V2: Why can I access outer function local variable in inner function?

28 Jun 2018, 04:30

I found it:
Force-local mode: If the function's first line is the word "local", all variable references are assumed to be local unless they are declared as global inside the function. Unlike the default mode, force-local mode has the following behavior:

Super-global variables (including classes) cannot be accessed without declaring them inside the function.
If the function is defined inside another function, the outer function's local and static variables cannot be accessed.
The LocalSameAsGlobal warning is never raised for variables within a force-local function.
You need to activate a force local mode to activate these effects.
Recommends AHK Studio
SAbboushi
Posts: 252
Joined: 08 Dec 2014, 22:13

Re: AHK V2: Why can I access outer function local variable in inner function?

28 Jun 2018, 08:37

Thanks! I keep thinking I've thoroughly read the related documentation, but it seems I missed the section on "Nested Functions"...

Does anyone know if this behavior (Variable List in debugger is not a complete list of variables accessible in current scope) is typical or not in debuggers? I would have expected not?

For anyone also trying to figure this out:
In my example, x and x1 of the outer function MAY be accessed within sub2(), but unless they ARE referenced in sub2(), SciTE4AutoHotkey does not list them as variables local to sub2().

So it seems that (according to SciTE4AutoHotkey), referencing sub1()'s variables in sub2() changes the scope of x and x1 to also be local to sub2().

I wrongly assumed that if x and x1 were accessible as local variables to sub2() that they would appear in the Debugger Variable List for sub2() whether I referenced them in sub2() or not, but now I know that I cannot rely upon the Variable List to be a complete list of variables accessible in the current scope.

Does anyone know if this behavior (Variable List in debugger is not a complete list of variables accessible in current scope) is typical or not in debuggers? I would have expected not?

---
And I offer my gratitude for the amazing AutoHotkey and to the many forum members who add such tremendous value to it. Thanks--
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: AHK V2: Why can I access outer function local variable in inner function?  Topic is solved

01 Jul 2018, 04:59

By default, a nested function may access non-dynamic local and static variables of any function which encloses it, but not ByRef parameters or variables which are created dynamically.
...
Dynamic variable references inside a nested function can resolve to variables from the outer function only if the nested function (or one of its own nested functions) also contains a non-dynamic reference to the variable.
If the nested function's source code does not contain a non-dynamic reference to the outer function's variable, that variable is not accessible to the nested function. All variables listed by the debugger should be accessible via dynamic referencing. For nested functions, adding a non-dynamic reference is akin to adding a variable declaration; in either case, the source code must be modified.

The debugger lists "imported" variables as a consequence of the implementation. What you see is actually not the outer function's local variable, but the inner function's alias, similar to a ByRef parameter.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Albireo, mikeyww, rommmcek, w_i_k_i_d and 44 guests