ColdFusion Implicit Struct Creation Cannot Use Quoted Identifiers
While I was working on my ColdFusion CSS Parser utility, I discovered that implicit struct creation cannot handle quoted identifiers. If you try to run this code:
<!--- Define styles. --->
<cfset objStyles = {
"font-size" = "12px",
"line-height" = "1.5em",
"font-style" = "normal"
} />
... ColdFusion will throw the following error:
Invalid CFML construct found on line 6 at column 20. ColdFusion was looking at the following text: {
The problem here is that the quoted identifiers, such as "font-size", are not valid variable names (which is what I think it requires). This is a minor issue, but if you want to create keys with non-variable-friendly names, you still have to rock the old school struct creation and value setting methodology:
<!--- Define styles. --->
<cfset objStyles = {} />
<cfset objStyles[ "font-size" ] = "12px" />
<cfset objStyles[ "line-height" ] = "1.5em" />
<cfset objStyles[ "font-style" ] = "normal" />
I really like the implicit struct and array creation, but there are definitely one or two tweaks that I really hope they can iron out before ColdFusion 9 comes out. Namely this, nested implicit structures, and passing implicit creation in method calls.
Want to use code from this post? Check out the license.
Reader Comments
I ran into this last week and it annoyed me a bit. I also was having issues creating implicit structs in a method call for some unit tests I was writing:
<cfset myCFC.myMethod({key1 = "value", key2 = "value2"}) />
I ended up refactoring it out to 2 lines along with a few other things, so I could be wrong, but I don't think you can this either. Have you run into this before as well?
@Brian,
Yes, you cannot pass implicit struct or arrays as part of method calls. This is definitely something that would be sweet-ass-sweet if it was fixed in ColdFusion 9.
@Ben - I definitely agree. Writing unit tests would be so much easier. I second the "sweet-ass-sweet".
:D
What would be even hellafied mo sweeta would be the ability to reference variable attributes as properties instead of through wrapper function calls. I think CF is ready for arraySweetAssSweetFeatures.length.
@David,
It will be very interesting to see if ColdFusion ever decided to go this way.
I don't think we're necessarily "that" far off.
The same point you just made could've been made for ++ and += syntax as well as the ability to use && and || a version or two ago.
Fingers crossed!