tracker issue : CF-4198565

select a category, or use search below
(searches all categories and all time range)
Title:

Be consistent with function statements inside closures

| View in Tracker

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:

The example above demonstrates `Lexical scoping` - similar example as above: ``` xa = a(10); ya = xa(100); writeDump(ya); function a(n) { function inner(factor) { return n * factor; }; return inner; } ``` The above works. The following fails: ``` xa = a(10); ya = xa(100); xb = b(20); yb = xb(100); writeDump(ya); writeDump(yb); function a(n) { function inner(factor) { return n * factor; }; return inner; } function b(n) { function inner(factor) { return n * factor; }; return inner; } ````
Comment by John W.
876 | April 28, 2017 12:10:29 PM GMT
Should be fixed. Please post *how* it's plannwed to be fixed before going ahead and fixing it though.
Vote by Adam C.
877 | April 28, 2017 12:10:37 PM GMT
Tested on ACF 2018,0,0,308164 and seeing the same behaviour.
Comment by John W.
27648 | May 02, 2018 08:04:52 PM GMT