[Functions] _ Function modifying Object sent as Parameter (Bug?) Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User
Posts: 407
Joined: 26 Jun 2017, 08:12

[Functions] _ Function modifying Object sent as Parameter (Bug?)

20 Jul 2018, 21:09

Hi,

In the example below, the "f()" function modifies the "x" object sent as parameter when it is called!

Is this a bug?

Code: Select all

x := []
x["Test"] := "xxxxxxx"

f(x)

msgbox, % x["Test"]
;returns "yyyyyyyyyy"
;it should return "xxxxxxx" instead!
;Is this a bug?


f(Options)	;___________ f (Function) ____________
{
Options["Test"] := "yyyyyyyyyy"
}
Partial Solution:

Code: Select all

x := []
x["Test"] := "xxxxxxx - script"

f(x)

msgbox, % x["Test"]


f(Options)	;___________ f (Function) ____________
{
Options := Options.Clone()
;"Clone()", Returns a shallow (not always a 100% clone???) copy of the object.
;"Clone()" creates a new object from the object referenced by "x" variable!
;then, "Options" variable will reference that newly created/cloned object!

Options["Test"] := "yyyyyyyyyy - function"

msgbox, % Options["Test"]
}
Last edited by User on 20 Jul 2018, 23:18, edited 1 time in total.
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: [Functions] _ Function modifying Object sent as Parameter (Bug?)

20 Jul 2018, 22:05

Do you happen to know how to create\clone a new object from the object referenced by a variable?
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: [Functions] _ Function modifying Object sent as Parameter (Bug?)

20 Jul 2018, 22:23

Sorry about my ignorance related to this but, "Returns a shallow copy of the object", that "shallow" what does it mean in this context?

I see no further explanations from the link!
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: [Functions] _ Function modifying Object sent as Parameter (Bug?)

20 Jul 2018, 22:43

Well, I will mark "Object.Clone()" as a solution for this! (A "shallow" solution, I may say!)

Thanks for helping!

(Obs:) I updated the main post of this thread with a partial solution!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: just me, Rohwedder and 181 guests