Skip to main content
Ben Nadel at Scotch On The Rock (SOTR) 2010 (London) with: Matthew Bourke and Jim Cumming
Ben Nadel at Scotch On The Rock (SOTR) 2010 (London) with: Matthew Bourke Jim Cumming

Safe Navigation Operator Doesn't Work With Brackets In Adobe ColdFusion 2025

By
Published in — Comments (2)

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.

Want to use code from this post? Check out the license.

Reader Comments

16,346 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.

16,346 Comments

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!

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
— Ben Nadel
Managed ColdFusion hosting services provided by:
xByte Cloud Logo