There's not much to say about this one. ColdFusion is finally playing catch-up, and standardising its approach to passing complex objects.
Traditionally, ColdFusion has passed "simple values" (strings, numerics, dates, etc) by value, and complex objects (structs, queries, XML) by reference (provided one understands "pass by reference" with these caveats: "Complex data-types in CF, and how they're not copied by reference"). The one exception was arrays. Arrays were passed by value. Why? I don't bloody know.
Right from the outset Railo decided that was bloody daft and has never done this, and this has been inherited by Lucee.
Now ColdFusion has caught up, even if I think the specific implementation is lacking.
There is a new setting for Application.cfc,
this.passArrayByReference
, which one can set to true (the default is false). If one sets that, then the behavour of passed arrays is changed.Let's have a look at some code running on ColdFusion 11:
function arrayToUpperCase(array){
for (var i=1; i <= array.len(); i++){
array[i] = array[i].ucase();
}
return array;
}
rainbow = ["Whero","Karaka","Kowhai","Kakariki","Kikorangi","Poropango","Papura"];
rainbowInUpperCase = arrayToUpperCase(rainbow);
writeDump(var=rainbow, label="rainbow", format="text");
writeDump(var=rainbowInUpperCase, label="rainbowInUpperCase", format="text");
This simple code has a function which takes an array, upper-cases each element and returns it. Afterwards, we dump both the original and returned arrays:
rainbow - array 1) Whero 2) Karaka 3) Kowhai 4) Kakariki 5) Kikorangi 6) Poropango 7) Papura
rainbowInUpperCase - array 1) WHERO 2) KARAKA 3) KOWHAI 4) KAKARIKI 5) KIKORANGI 6) POROPANGO 7) PAPURA
As you can see, when we modify the passed-in array, it does not impact the original array. Constrast this with the same operation with a struct:
function structToUpperCase(struct){
for (var key in struct){
struct[key] = struct[key].ucase();
}
return struct;
}
rainbow = {red="Whero", orange="Karaka", yellow="Kowhai", green="Kakariki", blue="Kikorangi", indigo="Poropango", purple="Papura"};
rainbowInUpperCase = structToUpperCase(rainbow);
writeDump(var=rainbow, label="rainbow", format="text");
writeDump(var=rainbowInUpperCase, label="rainbowInUpperCase", format="text");
rainbow - struct
BLUE: KIKORANGI
GREEN: KAKARIKI
INDIGO: POROPANGO
ORANGE: KARAKA
PURPLE: PAPURA
RED: WHERO
YELLOW: KOWHAI
rainbowInUpperCase - struct BLUE: KIKORANGI GREEN: KAKARIKI INDIGO: POROPANGO ORANGE: KARAKA PURPLE: PAPURA RED: WHERO YELLOW: KOWHAI
As you can see, because structs are passed by references, the argument in the function references the same struct as in the calling code, so changes to the argument are reflected in the original.