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

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

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 U.
1536 | November 01, 2016 04:17:20 PM GMT
+1 - This bizarre behavior warrants a look and a fix.
Vote by External U.
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
Vote by Andrew J.
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 C.
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 S.
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 C.
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 W.
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 D.
1546 | March 24, 2017 02:28:04 PM GMT
+1
Vote by Doug C.
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 W.
1540 | March 24, 2017 04:49:18 PM GMT
+1 - IsABug. Thanks!, -Aaron
Comment by Aaron N.
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.
Comment by Vamseekrishna N.
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!
Vote by Sean C.
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 W.
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 K.
29807 | October 19, 2018 03:51:46 PM GMT
Tested again with cffiddle this time on acf2018 update 1 - still broken https://cffiddle.org/app/29b1f255-34ca-488a-bbef-d3f0c8562cdb
Comment by John W.
30163 | January 31, 2019 08:13:48 PM GMT
Just noticed "Will keep you posted." above from Vamseekrishna was on the 04/03/2017 and nothing since?
Comment by John W.
30164 | January 31, 2019 08:15:53 PM GMT
Yeah.. I _really_ want to use CF's closures more often, but I'm tired of running into the issues. They really only work properly in simple scenarios. And, even then, not always consistently. CF's docs even say "scope your variables for performance", but variable scoping isn't even a workaround here. Thanks!, -Aaron
Comment by Aaron N.
30169 | February 01, 2019 09:38:34 AM GMT
Tested on ACF2018 update 3. No change in behaviour.
Comment by John W.
30432 | March 04, 2019 08:54:14 PM GMT
:( Tested on ACF2018 update 5. No change in behaviour. Still broken. C'mon Adobe - I raised this in 2016. At least acknowledge it is a bug and change the status to "Bug Verified", just ignoring it doesn't make it go away.
Comment by John W.
31464 | September 29, 2019 04:43:54 PM GMT
Hi John, This is a bug and we have already moved it to ToFix state. We will consider this bug for the next general update of ColdFusion. -Nimit
Comment by Nimit S.
31465 | September 29, 2019 05:50:18 PM GMT
Hi Nimit, Thanks for the update. It doesn't show as "ToFix" on the public facing tracker, but good to know that internally that is the status.
Comment by John W.
31476 | September 30, 2019 09:32:57 AM GMT