Did you know that you can specify in your returnType for a component method that it returns not just a component, but an array of components? To specify that you want to return a component of type Apple, you would normally do this:
returnType="Apple"
to specify an array of Apples, you simply do:
returnType="Apple[]"
You can also specify the same thing in cfargument. Here is an example:
<cffunction name="getApples" returnType="apple[]" output="false" access="remote"
hint="Get me some darn apples.">
<cfset var i = "">
<cfset var result = arrayNew(1)>
<cfloop index="i" from="1" to="10">
<cfset arrayAppend(result, createObject("component", "apple"))>
</cfloop>
<cfreturn result>
</cffunction>
<cffunction name="setApples" returnType="void" output="true"
hint="Just give me some apples.">
<cfargument name="appleData" type="apple[]" required="true">
<cfdump var="#arguments.appleData#">
</cffunction>
</cfcomponent>
Note: There is a bug with the built-in component viewer. It shows the type as Apple[], and when you click on it, you get an error. I had planned to do some updates to the component viewer so I will fix that as well. I need to ask Macromedia if I can then simply package the entire zip. (Technically it would be a copyright violation, but I'd have to have to package up my changes as a bunch of things to cut and paste.)
Archived Comments
So, you're saying that I can have a getter in one CFC return an array of objects returned from another CFC?
i.e.: I have an object book and an object author. book.getAuthor() returns author, but if there's more than one, it could return an author array?
Hey Ray, interesting post. What would this be good for?
This was added specifically to support web services with typed array.
Very interesting. Could this be used to support composition/aggregate multiplicity? My "vehicle" object could have a "wheels[]" property that contains a number of wheel objects.
I've been handling this with a property typed as an array that I then stuff with objects. It seems like the way you mention would be a lot clearer if it is as reliable.
Sean, you seem to know about this. Can you think of any pitfalls that might come up using this approach?
The *only* thing this syntax is meant to support is typed arrays in web service APIs. It doesn't mean anything outside of that context as far as I know.
Sean: I understand it was added for web services, but since it works perfectly well in components not being used as WS, are you still saying we shouldn't use? I wonder if we can get an 'official' ruling from MACR. Since it was in the release notes and not the official docs (afaik), it is kind of up in the air.
So Ray -
What happens if you try to pass out an array of Oranges, does it throw a typeing error? or does it just ignore it, and the Apples[] is more for semantic markup?
Nice find however!
Mark - it is smart enough to recognize that it isn't the right type.
An array of Apples.
What's on your mind Ray?
Since the type of the contents in an array cannot be specified when you create the array, does CF have to validate every item in the array at runtime? If so, wouldn't this be rather slow with a large array?
Steve: Even if so - if we didn't have this feature, I'd still have to check at runtime anyway.
Ray,
Did you ever find out if this was frowned upon by Adobe?
Nope.
I tried your code for the array of component, but it is not working. I get an error when trying to invoke the web service getApples from a cfm. Could you help me a little bit? Thank you very much.
AXIS error
Sorry, something seems to have gone wrong... here are the details:
Fault - [coldfusion.xml.rpc.SkeletonClassLoader$UnresolvedCFCDataTypeException : Could not resolve CFC datatype: apple[]][; nested exception is:
coldfusion.xml.rpc.CFCInvocationException: [coldfusion.xml.rpc.SkeletonClassLoader$UnresolvedCFCDataTypeException : Could not resolve CFC datatype: apple[]][
AxisFault
faultCode: {http://schemas.xmlsoap.org/...}Server.generalException
faultSubcode:
faultString: [coldfusion.xml.rpc.SkeletonClassLoader$UnresolvedCFCDataTypeException : Could not resolve CFC datatype: apple[]][; nested exception is:
coldfusion.xml.rpc.CFCInvocationException: [coldfusion.xml.rpc.SkeletonClassLoader$UnresolvedCFCDataTypeException : Could not resolve CFC datatype: apple[]][
faultActor:
faultNode:
faultDetail:
Sorry Jason - not sure why you would get that.