Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
Ben Nadel at CFUNITED 2009 (Lansdowne, VA) with: Todd Sharp
Ben Nadel at CFUNITED 2009 (Lansdowne, VA) with: Todd Sharp@cfsilence )

The invoke() Function Will Accept Three Kinds Of Argument Collection Formats In ColdFusion

By Ben Nadel on
Tags: ColdFusion

Earlier, when I was blogging about how the invoke() function can be used to dynamically invoke Java methods, I realized that the ColdFusion documentation didn't really explain how to format the "arguments" argument of the invoke() function. ColdFusion function invocation, in general, is extremely dynamic allowing for both named and ordered arguments as well as a sort of hybrid argument collection format. As such, I wanted to see which formats were supported by the invoke() function.

To test this, I created a simple user defined function (UDF) that would store a given value in a given struct. It's a completely useless function; but, it will allow us to look at all three types of argument collection formats:

  • Ordered arguments.
  • Named arguments.
  • Hybrid arguments (ie, ordered arguments using named arguments).

And, here's the test:

  • <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>

As you can see, the "arguments" argument of the invoke() function is being passed an array or ordered arguments, a struct of named arguments, and a struct of hybrid "ordered" arguments. And, when we run the above code, we get the following writeDump() output:

struct

foo: bar
hello: world
we be: jammin

That's kablamo! As you can see, the invoke() function successfully consumed all three argument collection formats. This is pretty cool. People often forget how magically dynamic ColdFusion can be.



Looking For A New Job?

Ooops, there are no jobs. Post one now for only $29 and own this real estate!

100% of job board revenue is donated to Kiva. Loans that change livesFind out more »

Reader Comments