Skip to main content
Ben Nadel at FirstMark Tech Summit (New York, NY) with: Jim Sullivan
Ben Nadel at FirstMark Tech Summit (New York, NY) with: Jim Sullivan

ColdFusion Implicit Struct Creation Cannot Use Quoted Identifiers

By
Published in Comments (7)

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

5 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?

15,902 Comments

@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.

167 Comments

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.

167 Comments

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!

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel