Elvis operator glitches on function returns

Description

It looks like the Elvis operator doesn't work quite right with function return values that are arrays. Repro:

This errors with:

It works as expected on ColdFusion.

My use case is that the result of a .reduce() call generally returns an array, but it might reduce down to an empty array, in which case I want to default the ultimate return value to an empty array, rather than the first item of the reduction.

Environment

None

Activity

Show:
Gavin Pickin
December 8, 2015, 11:51 PM

Just ran into this in 4.5.2.018

Sean Corfield
December 9, 2015, 2:01 AM

Just to check, if you say this instead, does it work on Lucee or still fail:

result = isNull( f()[1] ) ? f()[1] : "default";

(and, yes, I know that calls f twice but I want to check whether the issue is how ?: behaves or how the [1] behaves here)

I'm actually really surprised this works in ACF (and my personal opinion is that it should not – so I view this as a bug in ACF, not Lucee).

Brad Wood
December 9, 2015, 7:41 PM

Testing via the CommandBox REPL, isNull( f()[1] ) throws the same error (Element at position [1] doesn't exist in array). It seems at a first look that isNull checks will work on structs if you try and access a nonexistent key either via dot notation or "array" notation. isNull checks do not seem to work on actual arrays trying to access a non-existent index.

Sean Corfield
December 9, 2015, 10:05 PM

That's what I expected – and why I think ACF's behavior is wrong. With a struct you can expect a key to either be present or not (and some languages will not throw an error on myStruct[nonExistentKey], they'll just return some sort of null). With an array, an index is either in bounds (and you'll get a value back) or out of bounds (and you'll get an error). In general, languages treat structs and arrays very differently.

Assignee

Michael Offner

Reporter

Adam Cameron

Priority

Minor

Labels

None

Fix versions

None

Affects versions