Learning ColdFusion 9: Referencing Arrays Returned From Methods
Posted July 14, 2009 at 1:51 PM by Ben Nadel
This is a really minor syntax update in ColdFusion 9, but again, one that will probably remove a good amount of friction in certain areas. In ColdFusion 9, if you perform a method call that returns an array, you can now reference the array index directly off of the method call result without creating an intermediary variable. Meaning, you can now do things like this:
someMethod()[ 1 ]
Assuming that "someMethod" returns an array, the statement above will return the first element in the resultant array.
While this might seem like a rare scenario, it actually comes up all the time when using methods like REMatch() and XmlSearch(). To see this in action, let's take a look at an XmlSearch() example:
- <!--- Define an XML document. --->
- <cfxml variable="girls">
- Search for the name node and then get a reference to
- the first one returned in the results.
- <cfset nameNode = xmlSearch( girls, "//name" )[ 1 ] />
- <!--- Output name value. --->
- Name: #nameNode.xmlText#
Here, we are searching for the "name" node using XmlSearch(). XmlSearch() will return an array of all the nodes matching the given XPath query. We then take that resultant and, without an intermediary variable, grab the first node. Running the code above, we get the following output:
This is quite minor in scope, but very huge in payoff! Like I said with the implicit array / struct usage and the ternary operator, it's the small updates that can really have a profound effect over codability in the long run.
Technically, I don't think this is really an upgrade to the language - it's an update to the compiler (are these different things)? I say this because perviously, all the ingredients were already there: methods returned arrays and arrays could be directly accessed. The problem was that before ColdFusion 9 came along, this aspect of the code simply wouldn't compile:
Now that this combination of characters does compile, the functionality is naturally there.
Unfortunately, the reverse of this is not yet available:
If this could compile, then we'd be able to call methods on array indices as in:
component[ methodName ]()
Trying to do this will still throw the ColdFusion compile-time error:
Invalid CFML construct found on line X at column Y. ColdFusion was looking at the following text: (
Maybe they will rock that update for the final ColdFusion 9 release. While still a minor change, it would be quite major in terms of dynamic functionality.
What Other People Are Searching For
- Wanted: Full-Time ColdFusion Developer at Intoria Internet Architects
- Cold Fusion Senior Developer at Edge Information Management
- Back-End Web Developer-Information Technologist at Michigan State University
- ColdFusion Developer at Nonfat Media
- Mid-to-Senior Level Web Application Developer at SiteVision, Inc.
component[ methodName ]() would be great.
Yeah, it would be double super awesome.
Not that you would ever do anything like this, of course. Because, you know, every time you dereference a return value without error checking, God kills a kitten. With an ASP.NET reference manual.
@Rick lol! Fair enough. Though I like the .NET platform, that's still funny.
I really like this addition.
And it's about time, too.
As Rick pointed out, you just have to be careful w/this syntax because if your function does not return an item at the position you're reference, errors will be thrown.
This would throw an error because the array is empty.
I thought about that when I was writing this up. But, then I figured if you were going to be confident enough to directly reference a result, then you probably:
A) Are working with a situation that will return a result.
B) Have some sort of error handling set up to deal with an unexpected, "critical" error.
Of course, I probably should have mentioned it, nonetheless.
I second (third?) Adam and Ben.
component[ methodName ]() would be great.
I thought  was for arrays, wouldn't it be:
Square bracket notation is used for both array-index reference and *can* be used for struct-key reference:
array[ index ]
struct[ "key" ] === struct.key
Cool, learn something new everyday.