Ok, so today I was putting together an application.cfc in CFScript and I ran into an interesting problem. I was setting up my application variables, and I arrived at this.mappings where I needed to add a couple of custom locations. With tags it was easy enough:
<cfset this.mappings["/stuff"] = "C:\assets\resources\stuff">
<cfset this.mappings["/things"] = "C:\assets\lib\things">
And I know that in script I could have used:
this.mappings = {};
this.mappings["/stuff"] = "C:\assets\resources\stuff";
this.mappings["/things"] = "C:\assets\lib\things";
But I thought, wouldn't it be better to declare and initialise that struct at the same time? When I tried to look this up in the Adobe docs, I found the following syntax:
myStruct = {
key1 = "value1",
key2 = "value2",
key3 = "value3"
};
The problem is, in the case of mappings, my key names are all prefixed with a slash. So trying to write
this.mappings = {
/stuff = "C:\assets\resources\stuff" };
just doesn't work, since it's invalid syntax. Based on that, I supposed it just wasn't possible to do a declare and initialise in that situation.
Then I had a crazy thought...waaaay too crazy to work, but I gave it a shot anyway:
this.mappings = {
"/stuff" = "C:\assets\resources\stuff",
"/things" = "C:\assets\lib\things"
};
And that WORKS! To me, the syntax seems completely bizarre because it looks like we're assigning one literal string to another literal string. But somehow, the universe doesn't implode and ColdFusion interprets it just fine. As a bonus, the case of the keys is preserved just like when we use the square-bracket notation. Good stuff!
Does that seem really weird to anyone else?