bdw429s
05/19/2022, 6:30 PM<cfloop from="10" to="1" index="i">
foo
</cfloop>
It currently loops zero times.
• Should CF default the loop to step="-1"
when the from
is greater than the to
?
• How much existing code would break?bdw429s
05/19/2022, 6:35 PMzackster
05/19/2022, 6:36 PMbdw429s
05/19/2022, 6:36 PMzackster
05/19/2022, 6:40 PMbdw429s
05/19/2022, 6:41 PMDaniel Mejia
05/19/2022, 6:41 PM1--
so to me it makes sense that tag requires it too.bdw429s
05/19/2022, 6:41 PMloop from=array.len() to="1" ....
bdw429s
05/19/2022, 6:42 PMAdam Cameron
bdw429s
05/19/2022, 6:43 PMAdam Cameron
bdw429s
05/19/2022, 6:44 PMDaniel Mejia
05/19/2022, 6:44 PMzackster
05/19/2022, 6:44 PMAdam Cameron
bdw429s
05/19/2022, 6:44 PMbdw429s
05/19/2022, 6:45 PMbdw429s
05/19/2022, 6:45 PMAdam Cameron
10
and 1
. But those are expressions, so I think when they're literals then having special "I guessed what you actually meant" behaviour is... questionable.Adam Cameron
bdw429s
05/19/2022, 6:47 PMbdw429s
05/19/2022, 6:47 PMAdam Cameron
eachRight
function (akin to reduceRight
). But then I went "stop being lazy Adam... reverse the array first, then just use `each`". I think this is similar. Something things are already easy enough, they don't need to be made easier.Adam Cameron
I want to say "nooooo", but all my justifications for doing so are based on being dogmatic about "the way things are done".
Adam Cameron
bdw429s
05/19/2022, 6:49 PMAdam Cameron
Adam Cameron
bdw429s
05/19/2022, 6:51 PMAdam Cameron
myRange = [10..1]
) I would probably expect it to iterate downwards if I iterated over it. So... how is this different?Daniel Mejia
05/19/2022, 6:52 PMAdam Cameron
Adam Cameron
Daniel Mejia
05/19/2022, 6:54 PMAdam Cameron
zackster
05/19/2022, 6:54 PMAdam Cameron
Scott Bennett
05/19/2022, 7:15 PMScott Bennett
05/19/2022, 7:24 PMAdam Cameron
step="1"
everywhere.
Kill me now.
It's be the 2020s version of "closing" CFML tags.Adam Cameron
Adam Cameron
Adam Cameron
Adam Cameron
this.loopCounter = "modern"
or something.Scott Bennett
05/19/2022, 7:35 PMAdam Cameron
this.loopCounter = "evenModerner"
Mark Takata (Adobe)
05/19/2022, 7:41 PMseancorfield
loop from=array.len() to 1
would run for an empty array (from 0 to 1 -- so it would run twice, except it would likely blow up on indexing by zero) and for a single-element array (from 1 to 1), so the original code is broken in multiple ways.Scott Bennett
05/19/2022, 7:46 PMScott Bennett
05/19/2022, 7:49 PMloop from="1" to=array.len()
because that would make more sensebdw429s
05/19/2022, 7:50 PMDave Merrill
05/19/2022, 7:51 PMScott Bennett
05/19/2022, 7:51 PMScott Bennett
05/19/2022, 7:52 PMbdw429s
05/19/2022, 7:52 PMScott Bennett
05/19/2022, 7:52 PMScott Bennett
05/19/2022, 7:52 PMbdw429s
05/19/2022, 7:53 PMScott Bennett
05/19/2022, 7:53 PMScott Bennett
05/19/2022, 7:53 PMseancorfield
from > to
and it could well break a lot of code that expects such a situation to not loop!Adam Cameron
This thread just made a baby panda someplace cryMy work here is done.
Matt Jones
05/19/2022, 7:55 PMAdam Cameron
seancorfield
Adam Cameron
0
in the struct though. You can guess how well-prepared Lucee was for that...Adam Cameron
<cfscript>
// control
st = {1="one", 2="two", 3 ="three"}
result = arrayMap(st, (v, i) => "#i#:#v#")
writeOutput(serializeJson(result)) // ["1:one","2:two","3:three"]
writeOutput("<br>")
// example
st = {0="zero", 1="one", 2="two", 3 ="three"}
try {
result = arrayMap(st, (v, i) => "#i#:#v#")
writeDump(result)
} catch (any e) {
writeOutput(serializeJson([e.message, e.detail])) // ["can not set Element at position [0]","Index -1 out of bounds for length 3"]
}
</cfscript>
Full disclosure: I ran this on 5.3.7.47
. It seems to be fixed in the current version.Adam Cameron
Adam Cameron
5.3.9.133
bdw429s
05/19/2022, 8:21 PMAdam Cameron
Adam Cameron
seancorfield
Adam Cameron
Adam Cameron
Adam Cameron
bdw429s
05/19/2022, 8:23 PMbdw429s
05/19/2022, 8:24 PMwhile (it.hasNext()) {
k = it.next();
if (!Decision.isInteger(k.getString())) throw new ExpressionException("can't cast struct to an array, key [" + k.getString() + "] is not a number");
}
bdw429s
05/19/2022, 8:24 PM0
will pass thatbdw429s
05/19/2022, 8:24 PMStructAsArray.java
classseancorfield
bdw429s
05/19/2022, 8:25 PMseancorfield
Adam Cameron
st = [1="one", 4="four", 3 ="three"]
result = arrayMap(st, (v, i) => "#i#:#v#")
writeOutput(serializeJson(result))
bdw429s
05/19/2022, 8:26 PMScott Bennett
05/19/2022, 8:27 PMbdw429s
05/19/2022, 8:27 PMarr=[]
arr[2]='foo'
Adam Cameron
Adam Cameron
["1:one",null,"3:three","4:four"]
Adam Cameron
Adam Cameron
seancorfield
st = {1="one", 4="four", 3 ="three"}
and also for st = {"1"="one", "4"="four", "3" ="three"}
which categorically ought to be the equivalent of Map<String,String>
... sighbdw429s
05/19/2022, 8:29 PMseancorfield
st = createObject("java","java.util.HashMap");
st.put("2","two");
st.put("3","three");
st.put("5","five");
result = arrayMap(st, (v, i) => "#i#:#v#") // Can't cast Object type [java.util.HashMap] to a value of type [Array] on line 32
because, you know, heaven forbid that Lucee treats HashMap like Struct 🤣Adam Cameron
{}
when I meant to type []
in myArray.append({}, true)
and it worked fine in 5.3.7.47
but broke in 5.3.9.133
, and I started to dig into "WTF, Lucee?"Adam Cameron
bdw429s
05/19/2022, 8:33 PMelse if (o instanceof Struct) {
so it must be a CFML struct, a Map
won't do.Adam Cameron
seancorfield
st = createObject("java","java.util.HashMap");
st.put("2","two");
st.put("3","three");
st.put("5","five");
result = structMap(st, (v, i) => "#i#:#v#")
writeOutput("struct: " & serializeJson(result))
bdw429s
05/19/2022, 8:34 PMScott Bennett
05/19/2022, 8:35 PMAdam Cameron
Adam Cameron
Scott Bennett
05/19/2022, 8:41 PMAdam Cameron
Mark Takata (Adobe)
05/19/2022, 10:15 PM