[?] is there a preferred way of dynamically creating a new variable now, after the most recent v2 update?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
SandyClams
Posts: 63
Joined: 02 Jul 2020, 11:55

[?] is there a preferred way of dynamically creating a new variable now, after the most recent v2 update?

09 Mar 2021, 23:01

just wondering if there's something I missed. I'll sometimes loop over object properties or a list of strings and dynamically create local variables just for the convenience of not manually assigning them all, it's lazy but also pretty handy, would be sad to lose it.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: [?] is there a preferred way of dynamically creating a new variable now, after the most recent v2 update?

09 Mar 2021, 23:48

no, u havent missed anything. there straight up isnt a way of dynamically creating new variables now
a128 wrote:Current versions of the language do not permit creating new variables dynamically. This is partly to encourage best practices, and partly to avoid inconsistency between dynamic and non-dynamic variable references in functions.
lexikos
Posts: 9780
Joined: 30 Sep 2013, 04:07
Contact:

Re: [?] is there a preferred way of dynamically creating a new variable now, after the most recent v2 update?

10 Mar 2021, 05:21

N.B.
Variables cannot be created dynamically, but a variable can be assigned dynamically if it has been declared or referenced non-dynamically somewhere in the script.
Source: Variables and Expressions - Definition & Usage | AutoHotkey v2
Being unable to create a variable with a dynamic reference means that you must create it some other way, such as by simply including a non-dynamic reference to it somewhere in the script. If the change is preventing you from creating a variable dynamically, that means you were only ever accessing it dynamically. In that case, why weren't you using an array/object (directly)?


From v1,
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.
In v1, the actual source of the confusion was inconsistency between dynamic references, pseudo-arrays and non-dynamic references. I think the main issue wasn't the above, but the special case of pseudo-arrays:
For commands that create pseudo-arrays (such as StringSplit), each variable in the resulting pseudo-array is local if the assume-global mode is not in effect or if the pseudo-array's first element has been declared as a local variable
Users declare the first element as local/global so that they get a local/global pseudo-array, but then are unable to reference it non-dynamically because the individual elements are not declared. Or one might read or assign a global variable dynamically without declaration, but fail to do so non-dynamically. These issues were eliminated in v2.0-a002 by making dynamic references and commands which create pseudo-arrays consistent with non-dynamic references.

The "common source of confusion" was then (or at some point) removed from the documentation because it wasn't a common source of confusion anymore. It is not very clearly documented anywhere else, but there are hints:
Because the words local, global, and static are processed immediately when the script launches, [...]
A dynamic declaration such as global Array%i% is not possible, since all non-dynamic references to variables such as Array1 or Array99 would have already been resolved to addresses.
Source: Functions - Definition & Usage | AutoHotkey v2
Each script is semi-compiled while it is being loaded and syntax-checked. [...]
Each reference to a variable or function is resolved to a memory address, unless it is dynamic.
Source: Script Performance | AutoHotkey v2

Note that although you may still initialize your variables using a loop and dynamic references, if you only ever assign them dynamically and do not declare them local, they may end up being global.
Any variable reference in an assume-local function may resolve to a global variable if it is only read. However, if a variable is used in an assignment or with the reference operator (&), it is automatically local by default.
Source: Functions - Definition & Usage | AutoHotkey v2
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: [?] is there a preferred way of dynamically creating a new variable now, after the most recent v2 update?

10 Mar 2021, 06:03

lexikos wrote:
10 Mar 2021, 05:21
N.B.
Variables cannot be created dynamically, but a variable can be assigned dynamically if it has been declared or referenced non-dynamically somewhere in the script.
Source: Variables and Expressions - Definition & Usage | AutoHotkey v2
Being unable to create a variable with a dynamic reference means that you must create it some other way, such as by simply including a non-dynamic reference to it somewhere in the script. If the change is preventing you from creating a variable dynamically, that means you were only ever accessing it dynamically. In that case, why weren't you using an array/object (directly)?
thats a very astute observation, as always
the object property use case i kind of almost get, if you: the list of strings case i dont get at all. maybe u can show an example?
SandyClams
Posts: 63
Joined: 02 Jul 2020, 11:55

Re: [?] is there a preferred way of dynamically creating a new variable now, after the most recent v2 update?

10 Mar 2021, 10:16

@swagfag, it's the same general thing when I do it with strings. Sometimes I just find a use case where I can spare myself some manual typing or some perceived clutter by streamlining the creation of a few variables. Or I just want to be "clever"

Code: Select all

; for each given method name,
for (methodName in ['GetOwnPropDesc', 'DefineProp']) {
  ; set borrowed method from object prototype to local function variable
  %methodName% := Object.Prototype.%methodName%
}
@lexikos, it's not that I'm actually inhibited from the creation of the variable. I do later use the variable normally and so it still works. I just notice things have been switched up, also I'm triggering VarUnset warnings which is problematic, so I was hoping to find if there was some other, better practice as I do it
User avatar
kczx3
Posts: 1678
Joined: 06 Oct 2015, 21:39

Re: [?] is there a preferred way of dynamically creating a new variable now, after the most recent v2 update?

10 Mar 2021, 10:21

@SandyClams
It seems you're really looking for object destructuring from JS or extract/list from PHP, right?
SandyClams
Posts: 63
Joined: 02 Jul 2020, 11:55

Re: [?] is there a preferred way of dynamically creating a new variable now, after the most recent v2 update?

10 Mar 2021, 10:23

^^ haha, yep. Pretty much exactly that, or whatever the laziest and most feasible facsimile is
drozdman
Posts: 78
Joined: 05 Dec 2015, 01:07

Re: [?] is there a preferred way of dynamically creating a new variable now, after the most recent v2 update?

30 Apr 2023, 19:55

Variable assignment notations ":=" and "=" were very useful for creating variables dynamically. The quickness of it was the beauty of AHK. Though, the change is understandable.
Still...
Why is the notation ":=" maintained if there is no need for distinguishing it from literal assignments?
It would be sensible to use "=" only. Or add "=" and retain ":=" for compatibility issues.
That's what creates confusion in AHK when one switches to it from other programing languages. Existence of ":=" and "=" were not confusing to me at all from the beginning, btw (as it was suggested somewhere).
Necessity to write ":=" instead of "=" slows down the writing process.
User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: [?] is there a preferred way of dynamically creating a new variable now, after the most recent v2 update?

30 Apr 2023, 21:00

drozdman wrote: Why is the notation ":=" maintained if there is no need for distinguishing it from literal assignments?
It would be sensible to use "=" only. Or add "=" and retain ":=" for compatibility issues.
That's what creates confusion in AHK when one switches to it from other programing languages. Existence of ":=" and "=" were not confusing to me at all from the beginning, btw (as it was suggested somewhere).
Necessity to write ":=" instead of "=" slows down the writing process.
This was discussed a couple of years ago here (among other times like this much older thread referenced by lexikos), and it’s not up for consideration, as you’ll see in the previous discussion.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: DavidP and 28 guests