Using Custom Typed Arrays as Arguments

If you are writing a function in ColdFusion that accepts a custom typed array (com.hayes.project.component[] for example) and you don't want to make it required because it may not be provided, you can still use the default argument with an untyped array:

<cffunction name="myFunc">
<cfargument name="compArr" type="com.hayes.project.comp[]" required="false" default="#arrayNew(1)#" />
<cfreturn arguments />
</cffunction>

<cfdump var="#myFunc()#" />

I was worried that it may error out when it used the default function to create a new untyped Array. Once I thought about it, I decided to give it a try and sure enough it works great.

One thing to note, however, that Ray mentioned to me was that ColdFusion is lazy when it comes to validating typed Arrays. It only checks the first element in the array to make sure it is of the type you specified.

To overcome this shortfall, when you are looping through the array and accessing the elements' functions, simply wrap it in a try/catch in case the elements after the first one are not of the appropriate type.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)