Tuesday 8 May 2012

CFScript: Odd Struct Initialisation Syntax

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?

4 comments:

  1. Casino in the Netherlands - FilmFileEurope
    Online 윈윈 사이트 gambling has become an economic and political trend 토토 분석 방법 넷마블 in recent times, but 올레 벳 in 강친 닷컴 this context it is not only 먹튀 검증 소 a new concept, but also an opportunity to

    ReplyDelete
  2. What are the best Casino? - TrickToAction
    › pin › Casino › pin › Casino Find the best Casino: How are 사설 토토 사이트 추천 넷마블 casino players ranked? A casino is 파워볼 구간분석 벳무브 not the best when 꽁포 사이트 it comes to gambling. If you have 토토 총판 some 토토중계 experience with online gambling

    ReplyDelete
  3. How to Make Money from Betting on Sports Betting - Work
    (don't worry if you get it หาเงินออนไลน์ wrong, though) The ford fusion titanium process involves 바카라 placing wooricasinos.info bets on different events, but it can also be done by using herzamanindir.com/ the

    ReplyDelete
  4. SEGA GENESIS - GAN-GAMING
    SEGA 출장샵 GENESIS. ventureberg.com/ GENESIS-HANDS. Genesis (JP-EU). NA. 토토 NA. NA. https://sol.edu.kg/ SEGA febcasino.com GENESIS-HANDS. NA. SEGA GENESIS. NA. GENESIS-HANDS. NA.

    ReplyDelete