Day 7: ColdFusion 10 and Closure – Part 2

Yesterday take look on Closure with ColdFusion, now its time to discover more on this. 

Closure can be defined as inline without giving name. You can treat like variable and assigned it to variable scope, array or struct element or function return value. for ex.

[code:cf]defaultlogic = function(numeric a,numeric b) {return a + b; };[/code]

You can also defined inline as function or tag argument.
[code:cf]<cfscript>
function operation(numeric a, numeric b, function logic){
return logic(a,b);
}
writeoutput(operation(5,6,function(a,b) {return a+b;}));
writeoutput("<br/>");
writeoutput(operation(5,6,function(a,b) {return a-b;}));
</cfscript>[/code]

In above example which take three arguments and third one is closure where you can pass different logic to be performed on numbers. In next two statements operation function called where passed different closure which perform addition and subtraction respectively and output will be …

11
-1
But what if you want to keep addition as default logic in operation function, well function may be look like below.

[code:cf]<cfscript>
function operation(numeric a, numeric b, function logic = function(numeric x,numeric y) { return x + y;}) {
return logic(a,b);
}
</cfscript>[/code]
Unfortunately above code will not work and give syntax parsing error (I think Adobe should work on that). But you can assign closure in variable scope and then you can assign as default parameter, it may look like below.

[code:cf]<cfscript>defaultlogic = function(numeric a, numeric b) {return a+b;};
function operation(numeric a, numeric b, function logic = defaultlogic){
return logic(a,b);
}
writeoutput(operation(5,6));
writeoutput("<br/>");
writeoutput(operation(5,6,function(a,b) {return a-b;}));
</cfscript>
[/code]
First writeoutput statement will return addition even though we haven’t pass any value in third argument and function call will take default closure.

ColdFusion 10 added new function ‘isClosure’ to identify passed variable is closure or not and also modified isCustomFunction to return false in case closure passed. Below code will give you better idea about both functions.

[code:cf]
<cfscript>
x=10;
defaultlogic = function(numeric a, numeric b) {return a+b;};
function operation(numeric a, numeric b=5, function logic = defaultlogic){
return logic(a,b);
}
writeoutput("isCustomFunction call:<br/>");
writeoutput("variable x:" & isCustomFunction(x) & "<br/>");
writeoutput("closure defaultlogic:" & isCustomFunction(defaultlogic) & "<br/>");
writeoutput("function operation:" & isCustomFunction(operation) & "<br/>");
writeoutput("————————————————<br/>");
writeoutput("isClosure call:<br/>");
writeoutput("variable x:" & isClosure(x) & "<br/>");
writeoutput("closure defaultlogic:" & isClosure(defaultlogic) & "<br/>");
writeoutput("function operation:" & isClosure(operation) & "<br/>");
</cfscript>[/code]

And here is output

[code:cf]
isCustomFunction call:
variable x:NO
closure defaultlogic:NO
function operation:YES
————————————————
isClosure call:
variable x:NO
closure defaultlogic:YES
function operation:NO[/code]

In above example ‘x’ is variable containing numeric value and both functions will return false for it.