Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at cf.Objective() 2011 (Minneapolis, MN) with:

ColdFusion Implicit Struct Creation Cannot Use Quoted Identifiers

By Ben Nadel on
Tags: ColdFusion

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.



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?

Reply to this Comment

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

Reply to this Comment

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.

Reply to this Comment

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!

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.