Railo's Array.each() implementation behaves differently than ColdFusion's

Description

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

NB: I'm not saying it's intrinsically a bug because it doesn't follow CF's lead; at the very least the difference needs to be documented.

Details here: http://cfmlblog.adamcameron.me/2014/08/what-was-that-arrayeach-quiz-all-about.html

Bottom line: this code behaves differently on Railo from how it does on CF:

a = ["a"]; a[3] = "c"; a.each(function(v,i,a){ writeOutput("value: #v?:'null'#<br>"); });

Railo iterates over the elements, CF iterates over the indexes.

CF's implementation came first, so - unless there's a conscious decision to vary behaviour - I'd say this is a compat bug in Railo.

Environment

None

Activity

Show:

Adam Cameron 1 February 2015 at 16:03

Agreed on both counts.

Adam Cameron 1 February 2015 at 16:03

Comment from Micha on original ticket:

i personally think that i most cases the iterate value solution is better, but have the freedom to choose is for sure the best way.

Adam Cameron 1 February 2015 at 16:02

Sure, that makes sense. I'd also poll the community to see how much ppl rely on current behaviour as distinct from the existing behaviour: emulating CF might not be the best approach for Railo here anyhow. Perhaps it just needs a documentation change?

Adam Cameron 1 February 2015 at 16:02

Comment from Micha on original ticket:

i like the idea about "eachIndex", but changing the behavior of "each" can only happen with a new major release (5.0)

Adam Cameron 1 February 2015 at 16:02

If there was two types of functionality, I'd rather two methods, not an extra parameter.

Ruby has these:

  • each

  • each_index

Groovy also implements separate functions (Looping: closures):

  • each()

  • eachWithIndex()

They're not quite the same functionality, but demonstrate the approach. For Railo? Perhaps:

  • .each() - match ColdFusion, and mark it deprecated

  • .eachElement()

  • .eachIndex() - alias for .each()

NB: I'm just offering suggestions here, and pretty much thinking as I type. But I'd rather different methods than one method with a functionality toggle, which ain't very "Clean Code".

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:01
Updated 7 July 2022 at 05:40

Flag notifications