Skip to main content
Ben Nadel at CFCamp 2023 (Freising, Germany) with: Denis Burimov
Ben Nadel at CFCamp 2023 (Freising, Germany) with: Denis Burimov

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

By
Published in

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.

Want to use code from this post? Check out the license.

Reader Comments

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel