ColdFusion 9 language enhancements
As part of the Adobe User Group Tour 2009 I presented to Devon CFUG on ColdFusion 9 and ColdFusion Builder. As part of that presso I wrote a couple of simple demos on the script enhancements, which I thought I might as well post here. They are a bit contrived, but here they are!
Language enhancements
<cfoutput>
<h2>Assignment chaining example</h2>
<cfset c = "Assignment chaining example" />
<cfset a = b = c />
a = #a#<br />
b = #b#<br />
c = #c#<br />
<hr />
<h2>Pass implicit structures/arrays to tags and functions example</h2>
#ConcatArray( ["Pass","implicit","structures/arrays","to","tags","and","functions"] )#
<hr />
<h2>Direct access to elements of returned arrays example</h2>
#getExampleArray()[4]#
<hr />
<h2>Ternary operators</h2>
<cfset n1 = 10 />
<cfset n2 = 20 />
<!--- old way --->
<cfif n1 LT n2>
<cfset message = "Yes, n1 less than n2" />
<cfelse>
<cfset message = "No, n1 is not less than n2" />
</cfif>
<!--- old way alternative --->
<cfset message = IIF( n1 LT n2, DE("Yes, n1 less than n2"), DE("No, n1 is not less than n2") ) />
<!--- new way --->
<cfset message = n1 LT n2 ? "Yes, n1 less than n2" : "No, n1 is not less than n2" />
#message#
</cfoutput>
<cfscript>
function ConcatArray( required array myarray )
{
return ArrayToList( arguments.myarray, " " );
}
function getExampleArray()
{
return ["Direct","access","to","elements","of","returned","arrays"];
}
</cfscript>
cfscript enhancements
/**
* This is a JavaDoc style comment
* @output false
* @displayname JavaDoc Example
*/
component
{
/**
* I return a formatted salary with currency prefix
* @output false
*/
public string function CalculateFormattedSalary( required numeric hours, required numeric rate )
{
return "&##163;" & NumberFormat( CalculateSalary( arguments.hours, arguments.rate ), "9,99.99" );
}
/**
* I calculate the salary based on hours and hourlyrate
* @output false
*/
private numeric function CalculateSalary( required numeric hours, required numeric rate )
{
// award bonus if over 40 hours worked
var bonus = ( arguments.hours > 40 ) ? 99 : 0;
return arguments.hours * arguments.rate + bonus;
}
}
New and Import keywords
<cfscript>
// old way
John = CreateObject( "component", "model.package.User" ).init();
John.setFirstName( "John" );
John.setLastname( "Whish" );
John.setNickname( "Aliaspooryorik" );
WriteDump( John );
</cfscript>
Or...
<cfscript>
// importing packages
import model.package.*;
John = new User();
John.setFirstName( "John" );
John.setLastname( "Whish" );
John.setNickname( "Aliaspooryorik" );
WriteDump( John );
</cfscript>
Or...<br />
<cfscript>
// short hand which doesn't require import
Andy = new model.package.User();
Andy.setFirstName( "Andy" );
Andy.setLastname( "Beer" );
Andy.setNickname( "FlamingSquirrel" );
WriteDump( Andy );
</cfscript>
- Posted in:
- ColdFusion


Comment by Kirill – October 10, 2011