This one feels like a bug, but may just be the language operating as documented. The safe navigation operator — originally added using the (?.) syntax — works by evaluating both operands and returning null / undefined if either of them is undefined. In my post yesterday, on the structReshape() method, I used the safe navigation operator with bracket-based struct access (?[]). And it worked! But I think the fact that it worked is a bug, not a feature of Adobe ColdFusion 2025.
To be clear, I'm saying that the safe navigation operator working with bracket notation for struct access might be a bug because I believe the safe navigation operator is only documented to work with ?. syntax - never with brackets. So the fact that it works sometimes with brackets is the bug.
Supporting evidence for the "bug" mindset is that safe navigation bracket access doesn't even work consistently with structs. It only works if the struct variable is unscoped.
To demonstrate, I'll try to access two structs and one array:
- Struct as an unscoped variable.
- Struct as an scoped variable.
- Array as an unscoped variable.
Each of these three will use the ?[ bracket notation; and will output an error message if the safe navigation access fails:
<cfscript>
variables.dataAsStruct = {};
variables.dataAsArray = [];
variables.key = 3;
// Struct as UNSCOPED variable.
try {
writeOutput( dataAsStruct?[ key ] );
writeOutput( "Worked: Safe navigator unscoped struct access. <br>" );
} catch ( any error ) {
writeOutput( "Failed: Unscoped struct access ERROR! <br>" );
}
// Struct as SCOPED variable.
try {
writeOutput( variables.dataAsStruct?[ key ] );
writeOutput( "Worked: Safe navigator scoped struct access. <br>" );
} catch ( any error ) {
writeOutput( "Failed: Scoped struct access ERROR! <br>" );
}
try {
writeOutput( dataAsArray?[ key ] );
writeOutput( "Worked: Safe navigator array access. <br>" );
} catch ( any error ) {
writeOutput( "Failed: Array access ERROR! <br>" );
}
</cfscript>
When we run this in Adobe ColdFusion 2025 update 14, we get the following output:
- Worked: Safe navigator unscoped struct access.
- Failed: Scoped struct access ERROR!
- Failed: Array access ERROR!
As you can see, the first one worked. You can use the ?[ syntax to access a struct. But only if the struct is unscoped. Which — again — makes me think that "the bug" is that it works in the first place for any scenario.
Anyway, this is mostly a note-to-self to not get lured into the idea that I should ever use ?[ notation, even though it works in some access paths.
As an aside, the CFML code with ?[ won't even compile in Lucee CFML or Boxlang, let alone perform safe navigation access.
Reader Comments
As a minor aside, I'll note that in JavaScript, the safe navigation operator does work with brackets, but it uses the
?.[key]notation. I tried this in CFML and it doesn't even compile the template.There is an existing bug in the Adobe Tracker that further adds to the confusion: CF-4211887. In the 5-year old issue, it seems to be unclear as to whether bracket notation is "documented" at all.
There's also CF-4222394, which seems to indicate that bracket notation should work since Adobe classified said bug as "to fix". Granted this was several years ago, so I'm not sure what to think.
Post A Comment — ❤️ I'd Love To Hear From You! ❤️
Post a Comment →