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:

Here’s how it works for structures:

21 responses to “Implicit Array And Structure Creation In Scorpio”

  1. Ken Dunnington Avatar
    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. πŸ™‚

  2. Mike Kelp Avatar
    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.

  3. Rob Wilkerson Avatar
    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. πŸ™‚

  4. John Farrar Avatar
    John Farrar

    Ben, some novice will trip on your second example.
    structNew should be structNew()… right?

  5. tanguyr Avatar

    Hello Ben,
    Will this new syntax also work in cfscript?

  6. Steven Van Gemert Avatar
    Steven Van Gemert

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

  7. Joshua Curtiss Avatar
    Joshua Curtiss

    My, that second example is so… JSON-ish of you. πŸ˜€
    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. πŸ™‚

  8. charlie arehart Avatar
    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.

  9. Steve Nelson Avatar
    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?

  10. charlie arehart Avatar
    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.

  11. Rick Root Avatar
    Rick Root

    wow, I like this too!
    looks like CFMX8 is focused on the coders and administrators this time. yay!

  12. Steve Nelson Avatar
    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.
    {product="ColdFusion", version=8}
    {"product" : "ColdFusion", "version" : 8}
    Why two different variations? Ben any thoughts?

  13. Joshua Curtiss Avatar
    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! πŸ˜€ I’m not even sure if that would be useful, but it would be cool. πŸ™‚

  14. Joshua Curtiss Avatar
    Joshua Curtiss

    Add the verb "take" to my improper sentence above. πŸ™‚

  15. charlie arehart Avatar
    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.

  16. Ben Forta Avatar
    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

  17. shuns Avatar

    Great feature, have been waiting for this for some time πŸ™‚

  18. shuns Avatar

    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?

  19. charlie arehart Avatar
    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.

  20. James L Avatar
    James L

    So, this combined with the argumentscollection property should let us do something like the following? :
    <CFQUERY ARGUMENTSCOLLECTION="{name="myQuery", datasource="myCSN} ">

    (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)

Leave a Reply