Pritesh     Feb,08 2017

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.

defaultlogic = function(numeric a,numeric b) {return a + b; };

You can also defined inline as function or tag argument.

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

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 ...

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

<cfscript>function operation(numeric a, numeric b, function logic = function(numeric x,numeric y) { return x + y;}) {return logic(a,b);}</cfscript>

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.

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

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.

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

And here is output

isCustomFunction call:variable x:NOclosure defaultlogic:NOfunction operation:YES------------------------------------------------isClosure call:variable x:NOclosure defaultlogic:YESfunction operation:NO

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