-
1. Re: Is This What You Would Expect?
Adam Cameron. Jan 13, 2012 3:15 PM (in response to Dan Bracuk)Queries are copied by reference (for all intents and purposes), and simple values are copied by value. So when you do this:
myNewVar = someQuery
All you are doing is copying the reference, but the reference is still pointing to the same memory as the first one. So whether you access that memory by the SimpleDirectories reference or the DirectoriesWithSubDirectories reference, both are referencing the same memory space. If you change that memory via one reference or the other reference: the same bit of memory is being changed. Hence both variables seem "the same". Or changes to one seem to change the other.
On the other hand, strings are copied by value, so when ones goes:
y=x
Then y and x point to different parts of memory, but the = operation copies the data at x to the memory location starting at y. x continues to point to "fred" and when you subsequently put "barney" at the memory referenced by y, it does not impact the value of x.
CF - if memory serves - copies all complex variables except arrays by refererence. All simple values - and arrays - are copied by value.
It's worth googling "copy by reference" to get a better explanation than the one I have given. Also, some purists with point out that Java - and hence CF - copies everything by value, not reference. This is true, but with complex objects the value that is copied is the memory reference to the data, not the data itself. For all intents and purposes CF copies complex objects - except array - by reference.
As to why CF copies arrays by value? Who knows. I suspect it is a vagary of how arrays are dealt with in C++, which is what CF's early implementations were written in. There is no good reason that CF still does this, and indeed Railo copies arrays by reference (and it's faster in these operations accordingly). I have no idea what BlueDargon does, but my vague recollection is that it copies arrays by reference too. For other CF data types, all three platforms behave the same way (as far as I know).
--
Adam
-
2. Re: Is This What You Would Expect?
Owain North Jan 14, 2012 3:46 AM (in response to Adam Cameron.)As an addition, this is what ColdFusion's duplicate() function is for; it creates an identical copy of a complex object it would otherwise have copied by reference.
So if you did:
DirectoriesWithSubDirectories = duplicate(SimpleDirectories)
you'd end up with two distinct query objects.

