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

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Feb 4, 2008 at 12:47 PM // reply »
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?


Feb 4, 2008 at 12:55 PM // reply »
6,516 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.


Feb 4, 2008 at 1:08 PM // reply »
5 Comments

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


Feb 4, 2008 at 1:10 PM // reply »
6,516 Comments

:D


Feb 5, 2008 at 3:49 AM // reply »
65 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.


Feb 5, 2008 at 7:30 AM // reply »
6,516 Comments

@David,

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


Feb 5, 2008 at 1:11 PM // reply »
65 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!


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »
Nov 20, 2009 at 11:00 PM
Five Months Without Hungarian Notation And I'm Loving It
@Marcel, Yeah, I always err on the side of longer but more readable variable names. As for the camel casing of CF methods and the headless camel casing of custom items, I get around this by always ... read »
Nov 20, 2009 at 10:56 PM
Five Months Without Hungarian Notation And I'm Loving It
I use the following and love it: my.namespace.MyComponents.functionMethodsOrUDF() CONSTANT_VALUES_OR_PROPERTIES One thing I always try is to CamelCaseBuiltInColdFusionFunctions() so others can tell ... read »
Nov 20, 2009 at 5:38 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
Hi Ben, Great article. I've been looking around to see if ColdFusion image engine can programatically create the following "wrap around" effect: http://www.creativepro.com/article/photoshop-s-she ... read »
Nov 20, 2009 at 5:35 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Dave: I talked to Gert he suggested: <cfhttp method="get" url="http://{some cf website}" result="stuff" addtoken="yes" /> Note the addition of cfhttp attribute addtoken. That should persist y ... read »
Nov 20, 2009 at 5:23 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, Ahh, gotcha, yeah that makes sense. ... read »
Nov 20, 2009 at 5:17 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, sorry if I didn't make this clear. You can make it work like that if you want, just put <cfset session.foo = 1> (and <cfset application.foo = 1>) in your OnRequestStart() and it reve ... read »