Page 1 of 1

Functions / class methods - force-local mode

Posted: 29 Jul 2015, 04:26
by just me
The docs wrote:Within a function, any dynamic variable reference such as Array%i% always resolves to a local variable unless no variable of that name exists, in which case a global is used if it exists. If neither exists and the usage requires the variable to be created, it is created as a local variable unless the assume-global mode is in effect. Consequently, a function can create a global array manually (by means such as Array%i% := A_Index) only if it has been defined as an assume-global function.

Common source of confusion: Any non-dynamic reference to a variable creates that variable the moment the script launches. For example, when used outside a function, MsgBox %Array1% creates Array1 as a global the moment the script launches. Conversely, when used inside a function MsgBox %Array1% creates Array1 as one of the function's locals the moment the script launches (unless assume-global is in effect), even if Array and Array0 are declared global.

Source
v2-changes - Variables wrote:Double-derefs and pseudo-arrays created by commands (if any remain?) are now consistent with variables resolved at load-time. That is, a dynamic variable reference will resolve to a (non-super) global only if the function is assume-global or the variable is declared as global in the function. (Important: this also applies to the vVariable option of Gui.)

Functions default to either assume-local or must-declare depending on the setting at the point the function is defined, but this can be overridden by using Global, Local or Static without a variable name.

Source
That's why I wish an assume/force-local mode to be implemented preventing dynamic variable references in functions from resolving to global vars. The word Local without a variable name in the first line of the function could be used without breaking existing scripts, because it raises an error in the current v1 implementation.

Related: local Vars in Funktionen

Re: Functions: Assume/force-local mode

Posted: 27 Nov 2015, 09:52
by just me
Addition/modification/clarification:

Local should also prevent warnings like 'local var name same as global' and create a local variable even if a super-global variable with the same name exists. The latter would provide an easy way to prevent conflicts with super-global variables for functions designed to be included in other scripts (UDFs).

Related

Re: Functions / class methods - force-local mode

Posted: 23 May 2016, 03:27
by just me
I'm still missing it! Am I the only one?

Re: Functions / class methods - force-local mode

Posted: 23 May 2016, 03:49
by Gerdi
You are not the only one.

Re: Functions / class methods - force-local mode

Posted: 27 Aug 2016, 04:29
by just me
Any chance this will be implemented?

If you want to share functions / classes it's annoying to have to declare all local variables as 'Local' explicitly to avoid possible conflicts with super-globals defined in the user script.
Honestly, I'm too lazy to add all necessary 'Local' definitions to my existing scripts, but I'd add the new 'Local' statement asap.

Re: Functions / class methods - force-local mode

Posted: 27 Aug 2016, 18:16
by lexikos
There is some chance.

Re: Functions / class methods - force-local mode

Posted: 28 Aug 2016, 01:44
by just me
Thanks, I am waiting.

Re: Functions / class methods - force-local mode

Posted: 19 Aug 2017, 11:12
by just me

Re: Functions / class methods - force-local mode

Posted: 19 Aug 2017, 11:34
by Elgin
I second this request.

Re: Functions / class methods - force-local mode

Posted: 10 Dec 2017, 03:29
by jeeswg
just me wrote:If you want to share functions / classes it's annoying to have to declare all local variables as 'Local' explicitly to avoid possible conflicts with super-globals defined in the user script.
Honestly, I'm too lazy to add all necessary 'Local' definitions to my existing scripts, but I'd add the new 'Local' statement asap.
- I 1000% agree with this.
- Furthermore, if I do some edits to a function, and add a new variable, but forget to add it to the list of local variables, or remove a ByRef variable from the parameter list, but keep it in the function, I could therefore unwittingly make a function's variable global.
- Thus the main (only?) reason for defining individual variables as local, to prevent variable conflicts, in practice, actually makes things worse.
- Also, if you choose to add a variable from the body of a function, to the parameter list, you then have to remove its definition as a local variable.
- Also, I am often frequently making small edits to functions, and sometimes I need to make the same small edit to multiple functions, and adding a variable to various lists of local variables can be a bit awkward.
- Also, maintaining a list of local variables can be a bit awkward, ideally, they are kept in alphabetical order, and listed together in the same line to save code lines. Plus, you have to omit any parameter or static variables.
- It's very difficult to check that all of the variables in all functions have either been defined as local or global or static or as a parameter.
- Plus, if I don't add the local definitions, this annoys users who use #Warn.
- I understand that handling dynamic variables could pose more problems, and am not asking for this to be changed.
- I view this as the most important feature that, in my opinion, AutoHotkey is in need of.
Wish List 2.0 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 13&t=36789
- Thanks for reading.

Some related threads:
replace text maintain case - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 37#p171837
list functions and list local variables - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=35006

Re: Functions / class methods - force-local mode

Posted: 14 Dec 2017, 10:00
by guest3456
+1

Re: Functions / class methods - force-local mode  Topic is solved

Posted: 27 Dec 2017, 18:40
by lexikos
Shall this be marked solved as of v1.1.27.00?

I think the only part additional to the original request was:
[v1.1.27+]: Force-local mode can be combined with assume-static mode by specifying local and then static, as shown below. This allows the function to use force-local rules but create variables as static by default.

Code: Select all

global MyVar := "This is global"
DemonstrateForceStatic()

DemonstrateForceStatic()
{
    local
    static
    MyVar := "This is static"
    ListVars
    MsgBox
}

Re: Functions / class methods - force-local mode

Posted: 28 Dec 2017, 03:24
by just me
Perfect addition! It shall be marked as solved.