Calendar

««Jan 2012»»
SMTWTFS
1234567
891011121314
15161718192021
22232425262728
293031

Alert Email

Get a short email alert whenever a new entry is published.

Confidential, secure it's piece of cake to keep uptodate.

OpenBD gains "new x.y.z()" support

Published: 7:03 AM GMT, Friday, 26 March 2010

CFSCRIPT has been getting a lot of attention of late, bringing it more 'language' like, allowing you do more and more without needing to drop to tags. Ideal for business logic. OpenBD already supports many great enhancements to the CFSCRIPT notation, including many Javascript directives for declaring structures/arrays using the {key:[1,2,3,5],key2:{a:b}} syntax.

Another great addition has just made its way to the nightly build which is a more Java like way of creating your objects. Instead of using CreateObject(), you can now use the new Object() syntax, in much the same way you would declare objects in Java.

In addition to this, the unofficial/official constructor for CFC's, the init() function, is also called at object creation time, if found. Which incidentally means, that the init() function no longer needs to return back "this" which is the standard pattern in CFC creation at the moment. Naturally, you can still do that with no side effects.

An example of this new operator and the permutations can be seen here:

<!--- simple cfc path --->
<cfset obj = new test()>

<!--- full cfc path --->
<cfset obj = new full.path.to.test()>

<!--- cfc path as string --->
<cfset obj = new "test"()>

<!--- full cfc path as string --->
<cfset obj = new "full.path.to.test"()>

<!--- with arguments --->
<cfset t = new test( "foo", "bar" )>

<!--- with named arguments --->
<cfset t = new test( bar: "bar", foo: "foo")>

<!--- with args passed in as argumentcollection --->
<cfset args = { bar = 2, foo = 1 }>
<cfset t = new test( argumentcollection: args )>

All the usual path searching kicks in at this point (explicit support for import x; is coming soon).

You can start playing with this new syntax now, by downloading the nightly build.

Comments (6)

this is very nice, thanks!

It would be *great* to have support for java objects creation as well. Creating cfcs with java-style new and java objects with cf-style createObject('java',xxx) in our factories seems a bit silly :)

left by Olivier . Wednesday, 14 April 2010 2:04 PM

Good spot, I've fixed the example. It doesn't work with java classes but that might change in the future.

left by Andy Wu . Friday, 26 March 2010 9:08 PM

Looks awesome. Two things: In the top 3 cfsets there is an additional closing parenthesis before the closing of the tag, I'm assuming that that is a typo right? Also, I've dabbled with java within cfscript and whenever I pasted java code directly into cfscript, and there was a "new obj()", I would have to rewrite the syntax to make it cfscript compatible. Will this work now with raw java code or is it specifically for creating a cfc object? I see that import is coming soon too which leads me to believe that we are heading towards true java compatibility. This would just blow the roof!!!!

left by Rick . Friday, 26 March 2010 8:15 PM

Awesome, thanks!

left by Patrick Steil . Friday, 26 March 2010 1:43 PM

Correct, or in the top level customtags folder, or the root folder of the webapp folder. The wonders of the infamous "hunt-a-tag" madness that is CFML!

left by Alan Williamson . Friday, 26 March 2010 1:27 PM

So does this mean that to execute this statement:

<cfset obj = new test() )>

The test.cfc only need reside in the local folder of the script executing the cfset statement?

If so, that is cool and I have wondered from day 1 why you couldn't do that in ColdFusion... :)

patrick

left by Patrick Steil . Friday, 26 March 2010 1:25 PM
Add Comment