Confused by copy of object vs. clone()

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Relayer
Posts: 160
Joined: 30 Sep 2013, 13:09
Location: Delaware, USA

Confused by copy of object vs. clone()

09 Nov 2016, 16:06

I have been writing a lot scripts to do various things in Excel lately. I often save data in case of a desire to undo something that was done. SafeArrays and objects make for a convenient way to handle blocks of data.

The documentation clearly says that a copy of an object, say objB made from another object objA by using objB := objA actually produces two names that point to the same data in memory space. This is easily verified by observing that &objA equals &objB. Manipulation of the contents of objA are mirrored in objB as expected.

I was under the impression that a copy made with the clone() method is separate and indeed objC := objA.Clone() yields two different addresses in memory such that &objA != &objC. What surprised me is that manipulation of the contents of objA actually show up in objC as well. I'm using v1.1.24.02 64 bit. What is the correct way to create a copy of an object that will be independent of manipulations to the original object?

Relayer

Here's code that demonstrates what I'm seeing:

Code: Select all

#NoEnv

show( a  := { 1 : { 1 : "=$D$1-0.75/25" }
			, 2 : { 1 : "=$D$1-0.75/25" }
			, 3 : { 1 : "=$D$1-0.75/25" } }, "==================`nbefore`n==================`na" )

show( b := a, "b := a" )
show( c := a.Clone(), "c := a.clone()" )

Loop % a.Length()
	a[A_Index][1] := StrReplace(a[A_Index][1], "$")

show(a, "`n==================`nafter`n==================`nexpected `na")
show(b, "expected `nb")
show(c, "UNEXPECTED `nc = clone")

ExitApp
Escape::ExitApp


show(x, id)
{
	static var = ""
	var .= "`n" . id . " @ address = " . &x
	for k, v in x
		var .= "`n<" . k . ">" . v[1]
	var .= "`n---------------"
	Msgbox % var
}
just me
Posts: 9490
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Confused by copy of object vs. clone()

09 Nov 2016, 16:14

Object.Clone() "returns a shallow copy of the object". In other words, the method copies all key/value pairs into a new object. If the 'values' are objects (i.e. object references), they are copied also and reference the same object.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Confused by copy of object vs. clone()

09 Nov 2016, 16:27

Relayer wrote:What is the correct way to create a copy of an object that will be independent of manipulations to the original object?
Here's one way: https://autohotkey.com/board/topic/6954 ... /?p=440435
Also see lexikos' comment a bit further down on that page where he notes some optimizations and a "fatal flaw."
User avatar
Relayer
Posts: 160
Joined: 30 Sep 2013, 13:09
Location: Delaware, USA

Re: Confused by copy of object vs. clone()

09 Nov 2016, 16:33

Thanks guys.
lexikos
Posts: 9621
Joined: 30 Sep 2013, 04:07
Contact:

Re: Confused by copy of object vs. clone()

09 Nov 2016, 16:42

Relayer wrote:The documentation clearly says that a copy of an object, say objB made from another object objA by using objB := objA actually produces two names that point to the same data in memory space.
No, it clearly says they are the same object. A copy would be a separate object. Only the reference is copied, not the object.

objA and objB aren't objects; they are variables which (may) contain references to objects.
What surprised me is that manipulation of the contents of objA actually show up in objC as well.
Maybe you've figured it out: you weren't manipulating the contents of objA, but the contents of some shared object that both objA and objC (or the objects they refer to) referred to.

It's just the same as if you do objB := objA and change the contents of the object referred by objA. You aren't manipulating the contents of objA or objB, but the contents of an object which both variables refer to.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: peter_ahk, Rohwedder and 155 guests