Skip to main content
Ben Nadel at Scotch On The Rock (SOTR) 2010 (Munich) with: Michael Hnat
Ben Nadel at Scotch On The Rock (SOTR) 2010 (Munich) with: Michael Hnat ( @madmike_de )

Spread Operator Cannot Replace Struct-Based argumentCollection In ColdFusion

By on
Tags:

I've been slow to adopt the Rest and Spread operators in ColdFusion because they aren't supported in the version of Lucee CFML that I use at work. But, they are supported in my personal use of Adobe ColdFusion. As such, I wanted to start thinking about ways in which to leverage these operators. One such case is constructing a dynamic collection of arguments and then invoking a method with argumentCollection. I thought that perhaps I could use the spread operator instead. This works when the arguments are array-based; but, it does not work when the arguments are struct-based.

Spreading an array of dynamically collated arguments into a method invocation works quite nicely:

<cfscript>

	function echoArgs( a, b, c ) {

		writeDump( arguments );

	}

	// "Dynamically" constructed arguments ARRAY.
	dynamicArgs = [];
	dynamicArgs.append( "Awesome" );
	dynamicArgs.append( "Beautiful" );
	dynamicArgs.append( "Cantankerous" );

	// SPREADING arguments into method invocation (each item in the array becomes one of
	// the ORDERED arguments).
	echoArgs( ...dynamicArgs );

</cfscript>

Here, we're taking an array of values and we're spreading it / mapping it onto the set of ordered arguments in our method. And, when we run this Adobe ColdFusion code, we get the following output:

Arguments scope dumped to screen with expected output in ColdFusion.

As you can see, our array of inputs was neatly mapped to the ordered arguments in the function signature.

Of course, one of the most powerful features of ColdFusion is the fact that any method can be seamlessly invoked with either ordered or named arguments. As such, it's logical to think that the spread operator can be used to spread a Struct into a method invocation:

<cfscript>

	function echoArgs( a, b, c ) {

		writeDump( arguments );

	}

	// "Dynamically" constructed arguments STRUCT.
	dynamicArgs = {};
	dynamicArgs.a = "Awesome";
	dynamicArgs.b = "Beautiful";
	dynamicArgs.c = "Cantankerous";

	// First, demonstrate that this WORKS with the traditional argumentCollection.
	echoArgs( argumentCollection = dynamicArgs );

	// Second, demonstrate that SPREADING STRUCT arguments into a method invocation does
	// NOT work.
	echoArgs( ...dynamicArgs );

</cfscript>

Here, instead of dynamically constructing an array of arguments we're constructing a struct of arguments. I'm using this collection twice: first with the traditional argumentCollection approach; and then with the spread approach. And, when we run this ColdFusion code, we get the following output:

Arguments scope dumped to screen with unexpected output in ColdFusion.

As you can see, when we invoke the method with argumentCollection, our dynamically constructed struct of arguments maps cleanly onto the named parameters in the function. However, when we try to use the spread operator, what we get are iterator entries, not the mapped values.

The spread operator can be very powerful. Unfortunately, it doesn't appear to play nicely with named argument invocation of functions in ColdFusion. For that, I'll still be using argumentCollection.

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

Reader Comments

Post A Comment — I'd Love To Hear From You!

Post a Comment

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