Page 1 of 1

v2-changes doc: Numeric strings as keys

Posted: 25 Jul 2014, 12:56
by guest3456 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. 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.]

Re: v2-changes doc  Topic is solved

Posted: 25 Jul 2014, 20:31
by lexikos
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.