Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
Ben Nadel at cf.Objective() 2014 (Bloomington, MN) with: Emily Christiansen
Ben Nadel at cf.Objective() 2014 (Bloomington, MN) with: Emily Christiansen@egchristiansen )

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.




Reader Comments

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.