Blog

1May
2007
Implicit Array And Structure Creation In Scorpio

This one was also met with loud applause last night's Dallas usergroup presentation. Scorpio supports implicit array and structure creation. Here are some simple examples, first array:

view plain print about
1<!--- CFMX7 code --->
2<CFSET myArray=ArrayNew(1)>
3<CFSET ArrayAppend(myArray, "Adam")>
4<CFSET ArrayAppend(myArray, "Ben")>
5<CFSET ArrayAppend(myArray, "Tim")>
6
7<!--- In Scorpio you can use this code --->
8<CFSET myArray=["Adam","Ben","Tim"]>

Here's how it works for structures:

view plain print about
1<!--- CFMX7 code --->
2<CFSET myStruct=StructNew()>
3<CFSET myStruct.product="ColdFusion">
4<CFSET myStruct.version=8>
5
6<!--- In Scorpio you can use this code --->
7<CFSET myStruct={product="ColdFusion", version=8}>

Related Blog Entries

Comments (21)



  • Ken Dunnington

    Wow, a lot of these new features are really coming as a surprise (to me at least.) Can't wait to see what's left to reveal. :)

    #1Posted by Ken Dunnington | May 1, 2007, 10:32 AM
  • Mike Kelp

    This is a great feature. I know quite a few developers who will be really happy about this, though it is a small change.

    Oh and once again, thanks for a great presentation last night.

    Mike.

    #2Posted by Mike Kelp | May 1, 2007, 10:38 AM
  • Rob Wilkerson

    These are some great shorthand methods that we can finally use. Any consideration for adding another? Namely, ternary operators? That would be the cat's knees. :-)

  • John Farrar

    Ben, some novice will trip on your second example.

    structNew should be structNew()... right?

  • tanguyr

    Hello Ben,

    Will this new syntax also work in cfscript?

    Regs,
    /t

    #5Posted by tanguyr | May 1, 2007, 12:35 PM
  • Steven Van Gemert

    tanguyr, I'm pretty sure that was the driving force for this change. Yes it will work in CFScript.

    Steven

  • Joshua Curtiss

    My, that second example is so... JSON-ish of you. :-D

    I love it. Pretty soon, I'll have to do double-takes to make sure I'm looking at my JavaScript code or my CFScript code. :-)

  • charlie arehart

    Yep, John, that should indeed be structnew(). Good catch. I'm sure he was just writing the cf7 part of the example off the top of his head and hopefully will see these comments and correct it soon. Just confirming for you and other readers that you're spot on.

  • Steve Nelson

    This seems a bit confusing to me how this is used.

    For example, would the code below automatically convert this string to a structure?

    <cfset myStructSTRING='{product="ColdFusion", version=8}'>
    <cfset myStruct=myStructSTRING>

    How would you go from a CFML struct back to the JSON string?

    #9Posted by Steve Nelson | May 1, 2007, 02:38 PM
  • charlie arehart

    Steve, I think perhaps you're taking the reference to JSON by Joshua a little too literally. :-) It seems from the last part of your question that you're saying confusion could come because someone getting such strings as JSON inputs might want to convert them into a CF array with this syntax, but that's not what this feature is about. This is just about making it easier for CFML developers to create arrays and structs in a short-hand notation in traditional CFML code. There's no connection to JSON in it at all. Joshua was just saying it seems *like* JSON.

    What you've shown is just a simple assignment, and naturally, any quoted string on the right side of an assignment becomes a string in the left hand variable, and your string remains so in both assignments. (I suppose perhaps if this was a function, it might be able to take a string and coerce it into something else. )

    I'll leave it to others with more experience in the new JSON functionality in Scorpio to elaborate on whether your desired conversion (either direction) might be done with some other new functionality (if it can yet be discussed publicly). Hope that's helpful.

  • Rick Root

    wow, I like this too!

    looks like CFMX8 is focused on the coders and administrators this time. yay!

    #11Posted by Rick Root | May 1, 2007, 03:25 PM
  • Steve Nelson

    Charlie, you are right, this isn't JSON, it had me fooled! But indeed, it is not. It seems odd to me that they would make something that is 'close' but not the same as fairly standardized syntax. Sounds like a Microsoft thing to do.

    It's even more bizarre that you're saying CF8 (may?) have direct support for JSON *and* this almost-JSON syntax.

    CF8:
    {product="ColdFusion", version=8}

    vs:

    JSON:
    {"product" : "ColdFusion", "version" : 8}

    Why two different variations? Ben any thoughts?

    #12Posted by Steve Nelson | May 1, 2007, 07:24 PM
  • Joshua Curtiss

    Steve and Charlie: You know, if you could that code and turn it into this:

    <cfset myStructSTRING='{product="ColdFusion", version=8}'>
    <cfset myStruct=Evaluate(myStructSTRING)>

    That would blow my mind! :-D I'm not even sure if that would be useful, but it would be cool. :-)

  • Joshua Curtiss

    Add the verb "take" to my improper sentence above. :-)

  • charlie arehart

    Steve, as for me referring to CF8 and JSON, I'm only saying that there have been many public mentions of new JSON-oriented functionality coming in Scorpio (google for the keywords scorpio json), but am not meaning to propose anything more than what's been offered publicly. I was just saying that there MAY be some other solution for what you (and Joshua) sought. Perhaps Ben or others can speak to that. But it may be that even if there is, it's not yet public. Not trying to be cryptic. Just being frank.

  • Ben Forta

    John, you are correct, I missed the (). That's what happens when I blog in an airport about to board a flight. ;-) Thanks for pointing it out!

    --- Ben

    #16Posted by Ben Forta | May 1, 2007, 11:46 PM
  • shuns

    Great feature, have been waiting for this for some time :)

    #17Posted by shuns | May 2, 2007, 12:22 AM
  • shuns

    One question though - currently you have shown {product="ColdFusion", version=8} notice that for example - product = "coldfusion" the equals sign, shouldn't this be a colon ie - product: coldfusion ?

    This is how object literals work in other languages, isn't that what CF8 is trying to allow?

    #18Posted by shuns | May 2, 2007, 12:41 AM
  • Ben Forta

    Re JSON, see http://www.forta.com/blog/index.cfm/2007/5/2/Worki....

    --- Ben

    #19Posted by Ben Forta | May 2, 2007, 11:38 AM
  • James L

    So, this combined with the argumentscollection property should let us do something like the following? :

    <CFQUERY ARGUMENTSCOLLECTION="{name="myQuery", datasource="myCSN} ">
    ...
    </CFQUERY>


    (btw, I'm not a cf developer. Mostly flex, and from the looks of it, this is closer and closer to what I'm familiar with)

    #20Posted by James L | May 10, 2007, 04:46 PM
  • charlie arehart

    James, my answer would be "no, and heck no". :-)

    First, let me say that I couldn't get it to work as you propose. Now, you were missing a closing quote before your closing brace, but that's not the reason. I first used that new script-based struct-building code to create a struct, and confirmed it worked (dumped the struct it created). I then tried to use the same right-hand code (the brace-enclosed code) from that assignment to put into the ArgumentsCollection. It failed to compile. I tried all forms of quotes and pounds, to no avail.

    I also tried to put it in the VAR attribute of CFDUMP. It just wouldn't work. I also tried using evaluate. Maybe someone else can find a way to make it work.

    Even then, the "heck no" part of my reply is that I surely don't hope to see this style catch on. :-) Building a struct within an attribute of a tag this way seems very unusual, at least to a CF developer's eyes. I realize you're saying it seems flex-y. Still, I'd ask it it's really solving any problem or perhaps just creating more confusion. That's just my opinion, of course.