Saturday, May 17, 2008    
Home My Books Blog ColdFusion About Me Back    

Calendar
<< May 2007 >>
S M T W T F S
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    

Search

Categories
 • Adobe (61) [RSS]
 • AdobeMAX06 (45) [RSS]
 • AdobeMAX07 (59) [RSS]
 • AdobeMAX08 (6) [RSS]
 • AIR (96) [RSS]
 • Appearances (105) [RSS]
 • Books (66) [RSS]
 • CFEclipse (14) [RSS]
 • ColdFusion (1081) [RSS]
 • Flash (91) [RSS]
 • Flex (319) [RSS]
 • Jobs (81) [RSS]
 • JRun (12) [RSS]
 • Labs (27) [RSS]
 • LiveCycle (12) [RSS]
 • MAX (141) [RSS]
 • Regular Expressions (12) [RSS]
 • SQL (36) [RSS]
 • Stuff (492) [RSS]
 • Tips (CF Studio) (80) [RSS]
 • Tips (CF) (795) [RSS]
 • Tips (Dreamweaver) (91) [RSS]
 • Tips (Flex Builder) (2) [RSS]
 • Using CF (131) [RSS]
 • Wireless (96) [RSS]

Other BLOGs
 • Ray Camden
 • Tim Buntel
 • Sean Corfield
 • John Dowdell
 • Steven Erat
 • Brandon Purcell
 • Charlie Arehart
 • Full As A Goog

RSS Feeds
 • Feed
 • Subscribe

Join my mailing list and find out about new books and other topics of interest.

Thoughts, ideas, tips, musings, and pontifications (not necessarily in that order) by Ben Forta ...
NOTE: This is my personal blog, and the opinions and statements voiced here are my own.

Viewing By Entry / Main
May 1, 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:

<!--- CFMX7 code --->
<CFSET myArray=ArrayNew(1)>
<CFSET ArrayAppend(myArray, "Adam")>
<CFSET ArrayAppend(myArray, "Ben")>
<CFSET ArrayAppend(myArray, "Tim")>

<!--- In Scorpio you can use this code --->
<CFSET myArray=["Adam","Ben","Tim"]>

Here's how it works for structures:

<!--- CFMX7 code --->
<CFSET myStruct=StructNew()>
<CFSET myStruct.product="ColdFusion">
<CFSET myStruct.version=8>

<!--- In Scorpio you can use this code --->
<CFSET myStruct={product="ColdFusion", version=8}>

TrackBacks
There are no trackbacks for this entry.

No trackback URL. Trackbacks are only allowed via interactive form.

Comments
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. :)
# Posted By Ken Dunnington | 5/1/07 10:32 AM
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.
# Posted By Mike Kelp | 5/1/07 10:38 AM
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. :-)
# Posted By Rob Wilkerson | 5/1/07 11:29 AM
Ben, some novice will trip on your second example.

structNew should be structNew()... right?
# Posted By John Farrar | 5/1/07 11:45 AM
Hello Ben,

Will this new syntax also work in cfscript?

Regs,
/t
# Posted By tanguyr | 5/1/07 12:35 PM
tanguyr, I'm pretty sure that was the driving force for this change. Yes it will work in CFScript.

Steven
# Posted By Steven Van Gemert | 5/1/07 12:48 PM
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. :-)
# Posted By Joshua Curtiss | 5/1/07 1:58 PM
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.
# Posted By charlie arehart | 5/1/07 2:06 PM
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?
# Posted By Steve Nelson | 5/1/07 2:38 PM
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.
# Posted By charlie arehart | 5/1/07 2:57 PM
wow, I like this too!

looks like CFMX8 is focused on the coders and administrators this time. yay!
# Posted By Rick Root | 5/1/07 3:25 PM
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?
# Posted By Steve Nelson | 5/1/07 7:24 PM
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. :-)
# Posted By Joshua Curtiss | 5/1/07 7:27 PM
Add the verb "take" to my improper sentence above. :-)
# Posted By Joshua Curtiss | 5/1/07 7:28 PM
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.
# Posted By charlie arehart | 5/1/07 7:43 PM
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
# Posted By Ben Forta | 5/1/07 11:46 PM
Great feature, have been waiting for this for some time :)
# Posted By shuns | 5/2/07 12:22 AM
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?
# Posted By shuns | 5/2/07 12:41 AM
# Posted By Ben Forta | 5/2/07 11:38 AM
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)
# Posted By James L | 5/10/07 4:46 PM
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.
# Posted By charlie arehart | 5/10/07 5:30 PM

  © Copyright 1997-2008 Ben Forta, All Rights Reserved