Does ColdFusion have a short syntax for creating a struct?
coldfusion, struct, syntax
Solution
Coldfusion 8 (and up) has a struct literal notation:
<cfset objData = {
Key1 = "Value1",
Key2 = "Value2"
}>
However, there are a few strings attached:
- bennadel.com: "Learning ColdFusion 8: Implicit Struct And Array Creation"
- barneyb.com: "ColdFusion Struct Literals Fail Again"
- barneyb.com: "CF8 Structure Literal Gotcha"
Note: ColdFusion 9 fixed the errors outlined above, so with any CF version available nowadays you will be fine. I'm still leaving in the links for reference.
Starting with CF10, you can also use the syntax that's familiar from JavaScript:
<cfset objData = {
Key1: "Value1",
Key2: "Value2"
}>
Problem
Is there any "short" syntax for creating a struct in ColdFusion? I'd like to replace this verbose code: ``` <cfscript> ref = StructNew(); ref.Template = "Label"; ref.Language = "en"; stcML = GetPrompts(ref); </cfscript> ``` with something more like a JavaScript object: ``` <cfscript> stcML = GetPrompts({ Template: "Label", Language: "en" }); </cfscript> ``` Is there anything like this?