romanstr
12/19/2023, 10:29 PMloopArray there is a closure on arr, which outputs id. It was expected to cascade the id value from loopArray, but the variable from cfloop was output. Coldfusion outputs a variable from loopArray. In Lucee, I repeated it on versions 5 and 6 at https://trycf.com/. Is it bug or I expect something wrong?
<cfscript>
qry = queryNew("id,name");
queryAddRow(qry);
querySetCell(qry,"id",1);
querySetCell(qry,"name","John");
queryAddRow(qry);
querySetCell(qry,"id",2);
querySetCell(qry,"name","Mary");
function loopArray(id) {
var arr = [{
"value": "100"
}, {
"value": "200"
}];
writeOutput("<br>id outside closure: " & id);
arr.each(function(item){
writeOutput("<br>id inside closure: " & id);
});
}
cfloop (query=qry) {
loopArray(id = 777);
};
</cfscript>romanstr
12/19/2023, 10:31 PMfor(row in qry) {
loopArray(id = 999);
}romanstr
12/19/2023, 10:32 PMid outside closure: 777
id inside closure: 1
id inside closure: 1
id outside closure: 777
id inside closure: 2
id inside closure: 2Patrick
12/19/2023, 10:49 PMPatrick
12/19/2023, 11:07 PMarr.each are you attempting to get the 100 and 200 values? because id is not defined in that arrromanstr
12/19/2023, 11:10 PMarr only for make the closure example. The problem is that when I output "id" in function it's "777" (that value that I passed to function), but when I output "id" in nested closure it's "1" and "2" (value from cfloop). I expected value from parent functionPatrick
12/19/2023, 11:20 PMPatrick
12/19/2023, 11:20 PMCage S
12/19/2023, 11:54 PMcfloop , loopArray, and arr.each closure. Here, it seems as if Lucee is runing the arr.each closure function in the context `cfloop`/`qry` rather than in the context of loopArray.
If you specify arguments.id in loopArray (instead of just id), will you get 777 for all of your responses? If not, then I'd consider it a bug. If it does, question of whether it is proper functionality is still open. One could argue it should maintain compatibility with ACF.
(edit: use of arguments.id throws error that id doesn't exist in arguments scope, maybe because of context it's running in)romanstr
12/20/2023, 6:59 AMid from parent (loopArray)romanstr
12/20/2023, 7:05 AMcfloop query and for/in query should work same, but in for/in it works different.Adam Cameron
<cfset qry = queryNew("id,name", "integer,varchar", [
[1, "John"],
[2, "Mary"]
])>
<cfset id = "outside query loop">
<cfoutput>Outside loop: #id#<br></cfoutput>
<cfloop query="qry">
<cfset id = "inside query loop">
<cfoutput>Inside loop: #id#<br></cfoutput><!--- 1,2 --->
</cfloop>
In a query loop... the first "scope" checked is the query reference. Whether or not it's in a closure should be neither here nor there.
IMO Lucee's behaviour is correct here (the original). CF's is buggy.Adam Cameron
id is completely shadowed by the query... it just means... don't use the same variable name in situations like this. Same as in any other situation this arises.dswitzer
12/20/2023, 12:09 PMdswitzer
12/20/2023, 12:10 PMromanstr
12/20/2023, 12:10 PMloopArray when I use id it calls from arguments scope (writeOutput("<br>id outside closure: " & id)), not from cfloop. I expect that closure will check its arguments scope, after that parents arguments scope and after that cloop scope. Why Lucee checks arguments for function, but doesn't check it for closure?Adam Cameron
id (the arguments one) is not being shadowed by the same-named variable in the query any more. I did point that out. And this is entirely expected. I'm not sure what you are demonstrating with this.
@romanstr good point! Despite that being clear in your code, I overlooked it due to being a div 😉. Yeah fair cop in that case. My "simplified" example is showing something different, missing that nuance.Cage S
12/20/2023, 2:07 PMromanstr
12/20/2023, 2:13 PMAdam Cameron
dswitzer
12/20/2023, 3:57 PMCage S
12/20/2023, 4:53 PMAdam Cameron
romanstr
12/20/2023, 9:28 PM