[v2] local inner function in global outer function

Propose new features and changes
User avatar
Brujah4
Posts: 19
Joined: 11 Feb 2023, 03:27

[v2] local inner function in global outer function

13 Feb 2023, 11:59

Is it possible to have a local inner function in a global outer function?
That would be nice to get a kind of private inner function in a global function.
As far as I know this isn't possible, so I post it here as a wish.

My example:

Code: Select all

Init() {
	global
	var1 := 1
	var2 := 2
	(...)
	
	A_TrayMenu.Add "&Something", TrayMenuHandler
	
	TrayMenuHandler() {
		try {
			(...)
		} catch as err {
			(...)
		}
	}
}
In the code above the function TrayMenuHandler is only needed locally, so I wish it is a local inner function.
But as inner function the variable err is global and I don't want that.
Also, I don't want to have to init every variable with its own global.

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

Re: [v2] local inner function in global outer function

26 Feb 2023, 15:29

there used to be keyword local alone on the first line of a function to render it force-local, but that got removed around the time Lexikos made global variables universally accessible for reading without requiring any prior declarations, so idk if its likely for it to make a comeback
ntepa
Posts: 429
Joined: 19 Oct 2022, 20:52

Re: [v2] local inner function in global outer function

26 Feb 2023, 20:22

I just found out you can make a nested function not assume global by putting the global declaration after the nested function:

Code: Select all

Test() {
    inner() {
        InnerVar := 1
    }

    global
    GlobalVar := 2

    inner()

}

Test()
msgbox InnerVar
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: [v2] local inner function in global outer function

04 Apr 2023, 01:54

As with any assume-global function, if you want a variable to be local, all you need to do is declare it as such (local err on its own line).

@ntepa Probably best not to rely on this as it contradicts the current documentation, and there was never any intention for declarations to be positional.

@swagfag Force-local would prevent the inner function from referring to outer variables. It was removed because functions, even built-in functions, now follow the same rules as variables and would therefore need to be individually declared before they could be called. There is no "revert to assume-local" statement because it wouldn't make sense for a nested function to be implicitly allowed to refer to local variables of an outer function but not global variables that the outer function itself could refer to.

Brujah4 wrote:Also, I don't want to have to init every variable with its own global.
In a way, the design penalizes the use of global variables. If you make your code more modular or self-contained, there will be fewer global variables to declare or not declare, and less need for (or benefit to) assume-global.

On the other hand, does your function assign to these global variables? If not, you did not need to declare them in the first place.

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 63 guests