Using component as a variable type

Did you know that ColdFusion 8 adds "component" as a valid type to both the returnType attribute of cffunction and the type attribute of cfargument? What does this mean? The default "rule" for types is that if you do not specify a type recognized by ColdFusion (list provided at the bottom of this post), then ColdFusion assumes you mean a component. So for example:

<cfargument name="app" type="apple">

This line means that the app argument must be a component of type apple. It is also valid to pass in a component that extends apple, like greenapple.cfc. But again - what happened here was that ColdFusion didn't recognize apple and therefore considers it the name of a component.

ColdFusion 8 adds to the list of the supported types by adding "component". When you use component, you can pass any CFC instance. In the past, the only way to allow for any CFC would be to use "any". If we wanted to change that app argument to allow any CFC, we could use this:

<cfargument name="app" type="component">

For your reference, here is the list of supported types for cfargument/cffunction.

any Any variable type is allowed.
array An array is required.
binary Binary data is required.
boolean Boolean data is required.
component A component instance is required.
date Date data is required.
guid A valid GUID value is required.
numeric Numeric data is required.
query Query data is required. In case it isn't obvious, a query with no rows is still a valid query.
string String data is required.
struct Structure data is required.
uuid A ColdFusion UUID is required.
variableName The value must be a string and must be a valid ColdFusion variable name. It doesn't mean the variable has to exist, just that the variable name is valid.
void Speficies that no value is used. This only works in cffunction, not cfargument.
XML XML data is required.

One last note - you can also specify a component name and brackets. For example, apple[]. This means that an array of apple components is required. Do note though that ColdFusion only checks the first item in the array.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA