ColdFusion Implicit Struct Creation Cannot Use Quoted Identifiers

Posted February 4, 2008 at 12:12 PM

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:

 Launch code in new window » Download code as text file »

  • <!--- 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:

 Launch code in new window » Download code as text file »

  • <!--- 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.

Download Code Snippet ZIP File

Comments (7)  |  Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page



Keep your Web site content fresh and your overhead costs low with Savvy Content Manager

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?

Posted by Brian LeGros on Feb 4, 2008 at 12:47 PM


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

Posted by Ben Nadel on Feb 4, 2008 at 12:55 PM


@Ben - I definitely agree. Writing unit tests would be so much easier. I second the "sweet-ass-sweet".

Posted by Brian LeGros on Feb 4, 2008 at 1:08 PM


:D

Posted by Ben Nadel on Feb 4, 2008 at 1:10 PM


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.

Posted by David McGuigan on Feb 5, 2008 at 3:49 AM


@David,

It will be very interesting to see if ColdFusion ever decided to go this way.

Posted by Ben Nadel on Feb 5, 2008 at 7:30 AM


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!

Posted by David McGuigan on Feb 5, 2008 at 1:11 PM


Post Comment  |  Ask Ben


Home   |   Web Log   |   ColdFusion   |   Projects   |   Resume   |   Job Form   |   Search   |   Contact
Epicenter Consulting - Custom Software Solutions for Business Evolution HostMySite.com - The Leader In ColdFusion Hosting