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

Explicit Arguments Always Override ArgumentCollection In ColdFusion

By
Published in Comments (2)

In ColdFusion, you can use the argumentCollection keyword to propagate a set of predefined arguments to a function invocation. For those familiar with other programming languages, the argumentCollection is like a more flexible "spread" operator. When a function is invoked using argumentCollection, any and all of the parameters can be further overridden by an explicitly-provided argument. As a sanity check, I wanted to demonstrate to myself that it doesn't matter where the argumentCollection goes within the list of parameters.

Aside: ColdFusion tags expose a similar feature via attributeCollection.

To demonstrate, I'm going to invoke a method and provide the attributeCollection in between two other explicitly-provided arguments:

<cfscript>

	defaults = {
		a: "a_Default",
		b: "b_Default",
		c: "c_Default",
		d: "d_Default"
	};

	// In this test, notice that the "argumentCollection" comes in between two override
	// arguments. The placement of the "argumentCollection" is irrelevant - the explicit
	// arguments always "win".
	writeDump(
		testArgs(
			a = "a_Override",
			argumentCollection = defaults,
			c = "c_Override"
		)
	);

	// ------------------------------------------------------------------------------- //
	// ------------------------------------------------------------------------------- //
	
	// I simply echo the runtime arguments.
	public struct function testArgs( a, b, c, d ) {

		return { a, b, c, d };

	}

</cfscript>

As you can see, the argumentCollection is being used in between the a and c arguments. And, when we run this CFML in Adobe ColdFusion 2021, we get the following output:

CFDump of arguments showing that the ArgumentCollection only defined defaults for non-explicitly-provided arguments, regardless of where it was placed in the parameter list.

Both the a and c arguments used the explicitly provided value, despite the fact that one came before and one came after the argumentCollection. The placement of the argumentCollection is irrelevant — it only serves to provide defaults for non-explicitly-provided arguments.

This means that you can put the argumentCollection in the location that makes the code more readable. Sometimes, that might be mean putting it first to emphasize the propagation of values; or, it might mean putting it last to emphasize the overriding of values. Of course, you can put it where ever it makes the code most readable for you.

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

Reader Comments

16,109 Comments

ColdFusion just really makes working with functions so nice! It supports named parameters, ordered parameters, argumentCollection, and it the last few releases, the Spread and Rest operators. I freaking love this language!

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
Managed hosting services provided by:
xByte Cloud Logo