Status/Resolution/Reason: Needs Review//EnhancementRequired
Reporter/Name(from Bugbase): John Whish / John Whish ()
Created: 04/28/2017
Components: Language, Closures
Versions: 2016,11.0,2018
Failure Type: Incorrectly functioning
Found In Build/Fixed In Build: 2016,0,03 /
Priority/Frequency: Normal / Some users will encounter
Locale/System: ALL / Mac 10.10 64 bit
Vote Count: 2
Problem Description:
Consider the following code, this compiles and runs, returning the expected outcome (good).
{code:java}
<cfscript>
writeDump(a(10));
function a(n) {
function inner(factor) {
return n * factor;
}
return inner(100);
}
</cfscript>
{code}
Now consider this code:
{code:java}
<cfscript>
writeDump(a(10));
writeDump(b(10));
function a(n) {
function inner(factor) {
return n * factor;
}
return inner(100);
}
function b(n) {
function inner(factor) {
return n * factor;
}
return inner(1000);
}
</cfscript>
{code}
The code fails to compile because "Routines cannot be declared more than once.".
Either this code should compile and run (as the function statement name is unique *inside* the closure)
__or__
You should not allow function statements inside another function - so *both* examples should fail to compile and run.
You should force developer to write code to use function expressions (as below) and not partially support function statements in closures, or allow function statement to work in closures (as JavaScript does).
{code:java}
<cfscript>
writeDump(a(10));
writeDump(b(10));
function a(n) {
var inner = function(factor) {
return n * factor;
};
return inner(100);
}
function b(n) {
var inner = function(factor) {
return n * factor;
};
return inner(1000);
}
</cfscript>
{code}
My preference would be to support function statements in closures as then you can take advantage of function hoisting, making code cleaner and therefore the language nicer to use :)
Steps to Reproduce:
Run code examples above.
Actual Result:
Compiler error - but only on one example.
Expected Result:
Be consistent!
Any Workarounds:
Use function expressions.
Attachments:
Comments: