Bug! Pre increment/decrement cause abnormal behavior when used as key in associative array

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ShadowMaster85
Posts: 10
Joined: 21 Oct 2019, 13:26

Bug! Pre increment/decrement cause abnormal behavior when used as key in associative array

16 Nov 2019, 14:31

When using that code:

id := 0
items := Object(++id, "a", ++id, "b", ++id, "c", ++id, "d")

for key, values in items
msgbox % key " , " values

id := 10
items := Object(--id, "a", --id, "b", --id, "c", --id, "d")

for key, values in items
msgbox % key " , " values

Using either pre increment or pre decrement as associative array key only the last key-value pairs defined in the object ("d") are added, all the other values are ignored and not appears in the result array values, i need to use pre increment since i need the incremented value of the variable not the original value, anyway this is a bug since the same code above but with post increment or post decrement keys work correctly as described here https://www.autohotkey.com/docs/Variables.htm#Operators :
Pre- and post-increment/decrement. Adds or subtracts 1 from a variable (but in versions prior to 1.0.46, these can be used only by themselves on a line; no other operators may be present). The operator may appear either before or after the variable's name. If it appears before the name, the operation is performed immediately and its result is used by the next operation. For example, Var := ++X increments X immediately and then assigns its value to Var. Conversely, if the operator appears after the variable's name, the operation is performed after the variable is used by the next operation. For example, Var := X++ increments X only after assigning the current value of X to Var. Due to backward compatibility, the operators ++ and -- treat blank variables as zero, but only when they are alone on a line; for example, y:=1, ++x and MsgBox % ++x both produce a blank result when x is blank.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Bug! Pre increment/decrement cause abnormal behavior when used as key in associative array

16 Nov 2019, 17:09

as described, where? the excerpt ure quoting doesnt say anything about what would(or is meant to) happen when carrying out multiple prefix/postfix operations on a variable being passed of as a function argument during a function call
yeah i dont know about this being a bug. in other languages, this is usually undefined behavior. in AHK, it is currently undefined behavior. it could become defined behavior since AHK is windows only, but i dont see that happening it is defined behavior apparently

meanwhile u can use this workaround, or Array.Push() or whatever

Code: Select all

id := 0
items := {}
items[++id] := "a" 
items[++id] := "b" 
items[++id] := "c" 
items[++id] := "d"

for key, values in items
msgbox % key " , " values
Last edited by swagfag on 17 Nov 2019, 08:18, edited 1 time in total.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Bug! Pre increment/decrement cause abnormal behavior when used as key in associative array

17 Nov 2019, 00:02

This is documented,
Byref wrote: If a parameter in a function-call resolves to a variable (e.g. Var or ++Var or Var*=2), other parameters to its left or right can alter that variable before it is passed to the function. For example, MyFunc(Var, Var++) would unexpectedly pass 1 and 0 when Var is initially 0, even when the function's first parameter is not ByRef. Since this behavior is counterintuitive, it might change in a future release.
And not unique for object() function (or increment/decrement operation), udf example,

Code: Select all

v := 1
f(v,++v,++v,v++)
f(a,b,c,d){
	msgbox % a "`n" b "`n" c "`n" d
}
Cheers.

Edit, you can avoid passing the variable reference by performing an additional expression, such as ++var+0.
ShadowMaster85
Posts: 10
Joined: 21 Oct 2019, 13:26

Re: Bug! Pre increment/decrement cause abnormal behavior when used as key in associative array

17 Nov 2019, 11:02

Helgef wrote:
17 Nov 2019, 00:02
This is documented,
Byref wrote: If a parameter in a function-call resolves to a variable (e.g. Var or ++Var or Var*=2), other parameters to its left or right can alter that variable before it is passed to the function. For example, MyFunc(Var, Var++) would unexpectedly pass 1 and 0 when Var is initially 0, even when the function's first parameter is not ByRef. Since this behavior is counterintuitive, it might change in a future release.
And not unique for object() function (or increment/decrement operation), udf example,

Code: Select all

v := 1
f(v,++v,++v,v++)
f(a,b,c,d){
	msgbox % a "`n" b "`n" c "`n" d
}
Cheers.

Edit, you can avoid passing the variable reference by performing an additional expression, such as ++var+0.
Thanks for the clarification :) , anyway in this code:

; work correctly as expected, all items are added to array
id := 0
items := Object(id++, "a", id++, "b", id++, "c", id++, "d")

for key, values in items
msgbox % key " , " values

; abnormal behavior, only the last item is added to array
id := 0
items := Object(++id, "a", ++id, "b", ++id, "c", ++id, "d")

for key, values in items
msgbox % key " , " values

why this problem happends only when using pre increment or pre decrement and not happends when using post increment or post decrement ?
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Bug! Pre increment/decrement cause abnormal behavior when used as key in associative array

17 Nov 2019, 12:59

Post increment return the value (before the increment), not the variable after the increment. Even if this behaviour didn't occur, you'd get different results from post and pre increments, posts starts at zero, pre at one.

Cheers.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, Joey5, Rohwedder and 317 guests