tracker issue : CF-4197194

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

nested arrayEach parent execution context scoping issue

| View in Tracker

Status/Resolution/Reason: To Fix//Investigate

Reporter/Name(from Bugbase): John Whish / John Whish (John Whish)

Created: 11/01/2016

Components: Language, Closures

Versions: 2016,13.0

Failure Type: Non Functioning

Found In Build/Fixed In Build: CF2016_Update3 /

Priority/Frequency: Critical / All users will encounter

Locale/System: ALL / Mac OS 9.04

Vote Count: 14

Problem Description:

Seems to be a scoping issue when nesting arrayEach.

Steps to Reproduce:

[http://trycf.com/gist/4dacc80fad8cac47d889f5b112216498/acf2016?theme=monokai]

 
{code:java}<cfscript>
function doit() {
    var a = ["a", "b", "c"];
    var b = ["x", "y", "z"];
    var counter = 0;
    
    arrayEach(a, function(foo) {
        counter = 0;
        arrayEach(b, function(bar) {
            counter++;
            // in dump counter is always 0
            writeDump({
                counter: counter, 
                foo: foo, 
                bar: bar
            });
        });
    });
}
doit();
</cfscript>

{code}
Actual Result:

Counter is always output as having a value of 0

Expected Result:

Counter should increment per inner iteration

Any Workarounds:

Don't use arrayEach - use for each loop instead

----------------------------- Additional Watson Details -----------------------------

Watson Bug ID: 4197194

External Customer Info:
 External Company: 
 External Customer Name: John Whish
 External Customer Email: 
 External Test Config: My Hardware and Environment details:

Attachments:

Comments:

Hi John, Interestingly, writeDump(variables.counter) returns 1. Adobe, could you please explain the reported bizarre behavior? Thanks!, -Aaron
Comment by External User
1536 | November 01, 2016 04:17:20 PM GMT
+1 - This bizarre behavior warrants a look and a fix.
1543 | November 01, 2016 04:17:44 PM GMT
+1: This is definitely incorrect behaviour and is going to cause issues for many developers further down the line
1544 | January 06, 2017 11:09:33 AM GMT
you need to sort yer application of closure out. And don't just fix this specific ticket: check all the rest of the closure behaviour and make sure it works *everywhere*.
Vote by Adam Cameron
1545 | January 06, 2017 11:20:44 AM GMT
The scopes are being copied as per this doc. https://helpx.adobe.com/coldfusion/developing-applications/the-cfml-programming-language/extending-coldfusion-pages-with-cfml-scripting/using-closures.html This is not a bug and hence closing it as "AsDesigned"
Comment by Suchika Singh
1537 | March 20, 2017 06:52:16 AM GMT
Suchika: you don't understand what you're talking about, I'm afraid. All the references to `counter` are *the same variable*. That's how closure works. Check other languages as a reference. Here's the equiv in PHP: http://sandbox.onlinephpfunctions.com/code/2defddd623f09c38538765b138f8d2d4688ec156 <?php function doit() { $a = ["a", "b", "c"]; $b = ["x", "y", "z"]; $counter = 0; array_walk($a, function($foo) use (&$counter, $b) { $counter = 0; array_walk($b, function($bar) use (&$counter, $foo) { $counter++; // in dump counter is always 0 var_dump([ "counter" => $counter, "foo" => $foo, "bar" => $bar ]); }); }); } doit();
Comment by Adam Cameron
1538 | March 24, 2017 10:25:41 AM GMT
Javascript example for reference: function doit() { var a = ["a", "b", "c"]; var b = ["x", "y", "z"]; var counter = 0; a.forEach(function(foo) { counter = 0; b.forEach(function(bar) { counter++; // in dump counter is always 0 console.log({ counter: counter, foo: foo, bar: bar }); }); }); } doit();
Comment by John Whish
1539 | March 24, 2017 10:29:21 AM GMT
This is the completely incorrect way to use closures! It's in the name. It "closed" the environment in which the item was called. That is how they help.
Vote by Mark Drew
1546 | March 24, 2017 02:28:04 PM GMT
+1
Vote by Doug Cain
1547 | March 24, 2017 02:44:41 PM GMT
Please reopen this ticket. What Adobe has done is not in line with how the general programming community has accepted for closures to work in other languages. If this is by design, the design needs to be changed. It is important for closures in ColdFusion to work like closures in other languages.
Comment by Bradley Wood
1540 | March 24, 2017 04:49:18 PM GMT
+1 - IsABug. Thanks!, -Aaron
Comment by Aaron Neff
1541 | March 29, 2017 06:15:29 AM GMT
Thanks for the feedback. We are opening up this bug for evaluation. Will keep you posted.
1542 | April 03, 2017 09:41:09 AM GMT
Very bizarre behavior compared to every other language out there which supports closures. You need to fix this!
1548 | April 03, 2017 10:59:52 PM GMT
Tested against 2018,0,0,308164 and the behaviour has not changed. Please fix it :)
Comment by John Whish
27649 | May 02, 2018 07:59:37 PM GMT
Well, this ticket answers the question 'is there a single page I can read to decide whether to buy an upgrade license for Adobe Coldfusion?' Two years and closures broken in three commercial releases! Unbelievable.
Comment by Samuel Knowlton
29807 | October 19, 2018 03:51:46 PM GMT