Adobe ColdFusion Elvis Operator Struggles With Nested Array References

Adobe ColdFusion Elvis Operator Struggles With Nested Array References

[ad_1]

In Lucee CFML, the Elvis Operator (null coalescing operator) is moderately robust. Actually, it could actually regularly exchange the Protected Navigation operator when get entry to values on deeply-nested buildings. By contrast, the Elvis Operator in Adobe ColdFusion is a lot more problematic. And, actually, I simply bumped into every other tough edge within the ACF implementation. It kind of feels that the Elvis operator has hassle with nested array references.

Nested Struct references appear to be superb in each Lucee CFML and Adobe ColdFusion. Each engines will thankfully go back the empty-string for this nonsense expression:

( foo.bar.baz.kablamo ?: "" )

Alternatively, if we had been to switch a type of middleman undefined keys with an undefined array index, Adobe ColdFusion would smash. Let’s take a more in-depth glance and the way it breaks on the more than a few object paths.

In my Dig Deep Health app, while you carry out a exercise, I overlay the former units on best of the present units. In fact, if that is the primary time you may have carried out an workout, there is not any prior information to overlay. As such, I finally end up with an empty array; which I used to be making an attempt to soundly eat with the Elvis operator.

The code seemed one thing like this (closely boiled down):

<cfscript>

	recentWorkouts = [
		{
			sets: [
				{ weight: 100, reps: 12 }
			]
		}
	];

	writeDump( recentWorkouts[ 1 ].units[ 1 ].weight ?: "" );

</cfscript>

Right here, I am looking to get entry to the primary weight of the primary units of the primary recentWorkouts. Understand that each recentWorkouts and units is an array.

If we run the above “glad trail” code (the place the whole lot is outlined) in each Adobe ColdFusion and Lucee CFML, we get the next:

Now, let’s check out commenting-out the contents of the units:

<cfscript>

	recentWorkouts = [
		{
			sets: [
				// { weight: 100, reps: 12 }
			]
		}
	];

	writeDump( recentWorkouts[ 1 ].units[ 1 ].weight ?: "" );

</cfscript>

If we run this code in each engines, we get:

  • Lucee: [empty string]
  • ACF: [empty string]

Thus far, each engines are effectively dealing with the undefined units index. Now, let’s check out commenting-out the units completely:

<cfscript>

	recentWorkouts = [
		{
			// sets: [
			// 	// { weight: 100, reps: 12 }
			// ]
		}
	];

	writeDump( recentWorkouts[ 1 ].units[ 1 ].weight ?: "" );

</cfscript>

If we run this code in each engines, we get:

  • Lucee: [empty string]
  • ACF: Part SETS is undefined in a CFML construction referenced as a part of an expression.

Now let’s check out commenting-out the primary exercise:

<cfscript>

	recentWorkouts = [
		// {
		// 	// sets: [
		// 	// 	// { weight: 100, reps: 12 }
		// 	// ]
		// }
	];

	writeDump( recentWorkouts[ 1 ].units[ 1 ].weight ?: "" );

</cfscript>

If we run this code in each engines, we get:

  • Lucee: [empty string]
  • ACF: The machine has tried to make use of an undefined worth, which typically signifies a programming error, both for your code or some machine code. Null Guidelines are every other title for undefined values.

As you’ll be able to see, Lucee CFML continues to function correctly as we comment-out pieces farther up the chain of nested values. Adobe ColdFusion, however, begins to throw other mistakes relying at the stage of nesting.

ASIDE: Now not catching this within the code used to be my dangerous – I used to be sloppy in trying out my code after making adjustments.

I am so used to the usage of the Elvis / Null Coalescing operator in Lucee CFML as a result of it’s so tough and strong. I’ve to take into account that it has many extra rough-edges and gotchas in Adobe ColdFusion. Particularly, because it had been, with array references.

Need to use code from this put up?
Take a look at the license.



[ad_2]

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back To Top
0
Would love your thoughts, please comment.x
()
x