Visit Sponsor

Written by 8:54 am ColdFusion

Using StructFind for fun and profit

There is a very weird debate going on in the CF-Aussie list about the merits of the ColdFusion function structFind(). StructFind( struct, key ), is an aptly-named native function that fetches the value of the specified key, in the specified struct. If, for some reason, the key does not exist, it throws an error. (The docs are wrong)

Why would you ever need this function? One use case would be returning the value a key in a structure that is the result of a function evaluation. Take this example:







See the structFind call in the middle? It is operating on a structure returned from getDataMap() and the key as passed in the argument named ‘object’. ColdFusion doesn’t permit getDataMap()[arguments.object] syntax, so without needlessly breaking up this method, structFind is the Way.

It is important to note structFind will throw an exception if the specified key does not exist in the structure. This is exactly what I want it to do. If for some reason the key passed does not exist in the structure, failing fast is the only option.

I believe the ColdFusion language should support additional dynamic syntax such as:

structFun()[ keyname ]

or

Component["stringPartOfMethod" & variableContainingOtherPartOfMethod](args)
.

As a matter of fact, I personally made the request on the CF8 wishlist (see # 49) for just such a thing. Until my wishes have been granted, give me structFind or give me death!

Visited 1 times, 1 visit(s) today
[mc4wp_form id="5878"]
Close