Looping over Structs passed through the Arguments scope

This example came up while I was trying to refactor a for loop in to a StructEach. I was getting a weird error:

can't cast [orderNumber] string to a number value

All I was getting from the TagContext was a reference to the closure of the StructEach. After trying it a few different ways, I decided to turn to the trusty TryCF.com site to see what the issue was and if it was different across the various engines.

So, here’s our example snippet:

I’ve included a “Dump by passing struct directly:” section to compare the different results to what I would have hoped to see.

Let’s look at the results in Lucee:

TryCF Results of StructEach experiment

Yikes! The entire struct gets passed in as the key and the value is set to the index in the arguments array. You can imagine that errors out pretty quickly as you try to use the key as a string. (I was under the impression that the key had to be a string.)

As if this weren’t bad enough, Adobe ColdFusion implements it just a bit differently.

TryCF Results of StructEach experiment

First off, ACF11 doesn’t pass the entire array as the third parameter to the closure. πŸ‘Ž

Next, we see that β€” when passing the struct through the arguments of another function β€” instead of passing the filter name (ORDERNUMBER) as the key and the array of order numbers as the value, it passes the position as the key (1) and the struct as the value.

There’s one more example I want to cover. A simple for loop. You might be hoping this one acts better.

You’d be disappointed. 😞

Here’s the code:

Here’s the results from TryCF

For Lucee:

TryCF Results of for loop experiment

And ACF 11:

TryCF Results of for loop experiment

Still not what I would hope for, but at least it’s consistent. Β―\_(ツ)_/Β― (In fact, ACF is consistent between the for loop and the structEach loop. Good for them.)

As far as I can tell, this is due to the (strange, to me) handling of the arguments scope as both indexed and named. The easy way to solve this is…don’t pass the arguments scope containing a struct something you need to loop over that struct!

Also, I got to be honest, I’m not even sure where I’d start with reporting bugs on the different platforms. What are bugs and what aren’t? Which platform is doing it right (or at least less wrong)?

So I’m not going to touch that part; I’m going to stick to just making you aware of the pitfalls with using the arguments scope and looping over structs.

Looping over Structs passed through the Arguments scope

Leave a Reply

Your email address will not be published. Required fields are marked *