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

Propose new features and changes
just me
Posts: 5519
Joined: 02 Oct 2013, 08:51
Location: Germany

Functions / class methods - force-local mode

29 Jul 2015, 04:26

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
Last edited by just me on 23 May 2016, 03:24, edited 1 time in total.
just me
Posts: 5519
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Functions: Assume/force-local mode

27 Nov 2015, 09:52

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
just me
Posts: 5519
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Functions / class methods - force-local mode

23 May 2016, 03:27

I'm still missing it! Am I the only one?
User avatar
Gerdi
Posts: 102
Joined: 03 Aug 2015, 18:48
GitHub: grrdi
Location: Germany

Re: Functions / class methods - force-local mode

23 May 2016, 03:49

You are not the only one.
Win 10 Home (x64) or Win 7 Enterprise
https://github.com/Grrdi/ZackZackOrdner ... master.zip --> get folders on the quick
just me
Posts: 5519
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Functions / class methods - force-local mode

27 Aug 2016, 04:29

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.
lexikos
Posts: 6176
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Functions / class methods - force-local mode

27 Aug 2016, 18:16

There is some chance.
just me
Posts: 5519
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Functions / class methods - force-local mode

28 Aug 2016, 01:44

Thanks, I am waiting.
Elgin
Posts: 103
Joined: 30 Sep 2013, 09:19

Re: Functions / class methods - force-local mode

19 Aug 2017, 11:34

I second this request.
User avatar
jeeswg
Posts: 5174
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Functions / class methods - force-local mode

10 Dec 2017, 03:29

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
lexikos
Posts: 6176
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

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

27 Dec 2017, 18:40

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
}
just me
Posts: 5519
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Functions / class methods - force-local mode

28 Dec 2017, 03:24

Perfect addition! It shall be marked as solved.

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 2 guests