Cannot default first argument in reduce()'s callback

Description

Copied from https://issues.jboss.org/browse/RAILO-3147

This should work:
rainbow = ["Whero","Karaka","Kowhai","Kakariki","Kikorangi","Tawatawa","Mawhero"];
asString = rainbow.reduce(function(reduction="", colour){
reduction &= colour;
return reduction;
});
writeDump([rainbow,asString]);
Note how I am defaulting the reduction argument, instead of passing an empty string to reduce() as a starting value. Both approaches are valid (and supported in CF11).
However it errors with:
variable [REDUCTION] doesn't exist

Environment

None

Activity

Show:

Adam Cameron 14 July 2021 at 07:07

Cool, yeah. Good idea.

Pothys - MitrahSoft 7 July 2021 at 14:45

purposely I close this ticket because the test cases and info for the issue was included in https://luceeserver.atlassian.net/browse/LDEV-1296

Adam Cameron 6 July 2021 at 19:01

This is likely not a bug, just “unexpected behaviour”. It’s not an issue with the defaulting, it’s because when one doesn’t specify the initial value for that first parameter of the callback - via the second parameter of reduce, not via the callback’s default - the it would seem that Lucee is going “OK so that initial value is null then”.

The only question, perhaps, is that when I use this method signature of Lucee’s underlying implementation Array.reduce(callback) should Lucee be going “ah he means null for initialValue then” and then goes ahead and uses the Array.reduce(callback, initialValue) implementation, passing null as the value for initialValue? I can see arguments both ways for this.

Duplicate

Details

Assignee

Reporter

Priority

New Issue warning screen

Before you create a new Issue, please post to the mailing list first https://dev.lucee.org

Once the issue has been verified, one of the Lucee team will ask you to file an issue

Created 1 February 2015 at 16:04
Updated 14 July 2021 at 07:07
Resolved 7 July 2021 at 14:45

Flag notifications