Skip to main content
Inspiring
April 14, 2008
Question

When to use duplicate()

  • April 14, 2008
  • 9 replies
  • 714 views
What's the difference between:

<cfset x = y>

and

<cfset x = duplicate(y)>


Thanks,
Peter
This topic has been closed for replies.

9 replies

BKBK
Community Expert
Community Expert
April 16, 2008
Hi Ian,

Now now, not not.

Got it. I had an inkling it had to do with the devil in the machine.

Inspiring
April 15, 2008
BKBK wrote:
> Ian,
>
> Did you really mean to say, "Duplicating (cloneing) a variable copies the value to a new memory location so that it is not a separate entity.
> "
?
>
>

No, that should have said "..so that it is now a separate ...".
But once it was sent and I noticed my slip of a finger, there isn't a
way for me to edit the message from the newsgroup.

And I just despise the web interface, so I'm not going to go there just
for one letter.
BKBK
Community Expert
Community Expert
April 15, 2008
Ian,

Did you really mean to say, "Duplicating (cloneing) a variable copies the value to a new memory location so that it is not a separate entity.
"
?

Inspiring
April 14, 2008
A little addition to Adam's code to make it more implicit on what is
happening.

{code}
<cffunction name="showIt">
<cfargument name="aVar">
<cfargument name="aLable">

<cfdump var="#arguments.aVar#" label="#arguments.aLable#">
</cffunction>

<cfscript>
st1 = structNew();
st1.firstName = "Adam";
st1.lastName = "Cameron";
showIt(st1,"ST1");
writeOutput("<hr/>");

st2 = st1;
showIt(st1,"ST1");
showIt(st2,"ST2");
writeOutput("<hr/>");

st2.firstName = "Someone";
st2.lastName = "Else";
showIt(st1,"ST1");
showIt(st2,"ST2");
writeOutput("<hr/>");

st3 = duplicate(st1);
showIt(st1,"ST1");
showIt(st2,"ST2");
showIt(st3,"ST3");
writeOutput("<hr/>");

st3.firstName = "Third";
st3.lastName = "Person";
showIt(st1,"ST1");
showIt(st2,"ST2");
showIt(st3,"ST3");
</cfscript>
Inspiring
April 14, 2008
Thanks Ian and Adam!

This makes sense.

After looking at the code examples, I can say that when working with complex variables, it is important to use the duplicate function when copying values to another complex variable or you could potentially and unintentionally alter your original values.


When working with simple variables, using duplicate is not necessary. See my test code:
Inspiring
April 14, 2008
> Yes, I did read the docs.

OK, was just checking if you'd tried to help yourself. Heaps of people
here do not.

This code demonstrates copy-by-value (duplicate) vs copy-by-reference
(assignment operator).

{code}
<cfscript>
st1 = structNew();
st1.firstName = "Adam";
st1.lastName = "Cameron";

st2 = st1;
st2.firstName = "Someone";
st2.lastName = "Else";

st3 = duplicate(st1);
st3.firstName = "Third";
st3.lastName = "Person";
</cfscript>

<cfdump var="#variables#">
{code}

st1 and st2 are both references to the same object. st3 is a different
object than st1, created with the same initial values.

Make sense?

--
Adam
Inspiring
April 14, 2008
> The former is done to save memory and ColdFusion does this with complex
> variable types such as arrays, structures and objects. It does not do
> this with simple variables such as numbers and strings.

Arrays, as far as I know, are copied by value. Not that intuitive given
how CF seems to copy "all" other complesx types by reference.

--
Adam
Inspiring
April 14, 2008
SpiderFromMars wrote:
> Yes, I did read the docs. It talks about returning a clone. But, my question
> is: What is the difference between returning a clone and between referencing
> the variable directly. What is the benefit of it?
>


Two variables that are a reference are pointing to the same value in the
same memory location. Thus if you change the value with variableA, then
the value of variableB changes.

Duplicating (cloneing) a variable copies the value to a new memory
location so that it is not a separate entity.

The former is done to save memory and ColdFusion does this with complex
variable types such as arrays, structures and objects. It does not do
this with simple variables such as numbers and strings.

Inspiring
April 14, 2008
Yes, I did read the docs. It talks about returning a clone. But, my question is: What is the difference between returning a clone and between referencing the variable directly. What is the benefit of it?
Inspiring
April 14, 2008
> What's the difference between:
> <cfset x = y>
> and
> <cfset x = duplicate(y)>

As a first port of call: did you read the docs?

http://livedocs.adobe.com/coldfusion/8/functions_c-d_47.html

--
Adam