Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created December 31, 2015 12:59
Show Gist options
  • Save bennadel/9881efad03522d44c322 to your computer and use it in GitHub Desktop.
Save bennadel/9881efad03522d44c322 to your computer and use it in GitHub Desktop.
The invoke() Function Will Accept Three Kinds Of Argument Collection Formats In ColdFusion
<cfscript>
// I am the collection being added-to.
collection = {};
// Approach One: Define arguments as an ordered array.
argsArray = [ collection, "foo", "bar" ];
// Approach Two: Define arguments as a named collection.
argsStruct = {
collection: collection,
key: "hello",
value: "world"
};
// Approach Three: Define arguments as a named collection in which the keys are
// intended to represent an ordered collection.
argsMixed = {
"1": collection,
"2": "we be",
"3": "jammin"
};
// Invoke the addTo() method using all three argument types.
invoke( "", "addTo", argsArray );
invoke( "", "addTo", argsStruct );
invoke( "", "addTo", argsMixed );
writeDump( var = collection, format = "text" );
// ------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------- //
/**
* I add the given value to the given collection using the given key.
*
* @collection I am the collection being mutated.
* @key I am the key being used to store the value.
* @value I am the value being stored.
* @output false
*/
public struct function addTo(
required struct collection,
required string key,
required any value
) {
collection[ key ] = value;
return( collection );
}
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment