v2-changes doc: Numeric strings as keys Topic is solved

Discuss the future of the AutoHotkey language
Posts: 3134
Joined: 09 Oct 2013, 10:31

v2-changes doc: Numeric strings as keys

25 Jul 2014, 12:56

http://ahkscript.org/v2/v2-changes.htm wrote: Objects
Numeric strings are now treated as strings, not integers. For instance, x[1] is not equivalent to x[n] where n contains "01" or "1", and the string keys "01" and "1" are no longer equivalent.
http://ahkscript.org/v2/v2-changes.htm wrote: Types
{1:0, "1":0} produces an object with one key-value pair instead of two, since both keys are now interpreted as integers.
I think the two quotes above from the v2-changes doc contradict each other, unless I'm misunderstanding something.

Code: Select all

msgbox, %a_ahkversion%

obj := {1:0, "1":0}

for k,v in obj
   msgbox, %k%=%v%
in both 1.1 and 2.0, I'm seeing two k/v pairs

[Note from Lexikos: Renamed topic.]

Posts: 7057
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: v2-changes doc  Topic is solved

25 Jul 2014, 20:31

Yes, the second one is obsolete. In v1, {1:0, "1":0} produces two key-value pairs because the "is this numeric" checks always consider quoted/literal strings to be non-numeric. In other words, x["1"] always assigns a string key while x[v:="1"] assigns a separate numeric one. This inconsistency was removed in v2 by changing expressions to treat quoted/literal strings the same as other strings. Consequently, quoted strings can be used in numerical expressions - while that doesn't sound useful, it is in the case of ("0x" hex) + 0, which doesn't work in v1.

To put it another way, the "is this numeric" checks now treat quoted strings differently, but as of more recent alpha builds, objects no longer perform those checks anyway since they don't care whether a string is numeric.

If a numeric string key is passed via a variable or the return value of a function, it is treated as a number in v1, but not in v2.

Thanks for pointing it out.

Edit: It appears I had corrected that already, but the online copy is out of date.
Objects treat strings and pure integers differently; string keys, even numeric ones, are separate from pure integer keys. So `{1:0, "1":0}` still produces an object with two key-value pairs, but unlike v1, passing these values via variables does not change the behaviour.

Return to “AutoHotkey v2 Development”

Who is online

Users browsing this forum: No registered users and 6 guests