Explicit Arguments Always Override ArgumentCollection In ColdFusion
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:

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
This is probably as it should be, but also surprising and not as I would have expected.
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! ❤️