Quoted from the Adobe ColdFusion 10: Using closures documentation:
function helloTranslator(String helloWord) { return function(String name) { return "#helloWord#, #name#"; }; }
How to scope helloWord and name properly on the return line? Are they both in Arguments scope? If that’s the case, they must be unique?
The Closures and functions section also mentions there are 4 more scopes on top of the already long scope search:
In closure, following is the order of search for an unscoped variable:
- Closure’s
localscope- Closure’s
argumentsscope- Outer function’s
localscope if available- Owner function’s
localscope if available- ColdFusion built-in scope
If I scope something as 'local.', would it search 1 only, or 1,3 & 4?
BTW, I understand Closure & Outer. Who is Owner?
Thank you
Update: Enhancement Request filed: ColdFusion 10.0 – Feature 3191742
Here
helloWordandnamecannot be scoped. There is an implicitOwnerscope with “closures defined within a function” which is the local scope of declaring (parent) function, where these variables are present. So these variables must be unique (within function) to be accessed from closures.In closure, search for an unscoped variable goes through:
If something is scoped as
Local, in a closure, it would search in 1 only. AFN there is no way to directly scope for 3,4.p.s. as said earlier
Ownerscope is nothing but an implicit scope which points to a cached copy of parent/outer function’s local scope at the time of closure’s creation.p.s. You may log an enhancement with ColdFusion to make this scope explicit.
An example of different scopes is as follows. Run this and you shall understand how closure would use differnt scopes.