Skip to main content
Ben Nadel at Scotch On The Rocks (SOTR) 2011 (Edinburgh) with: Tom Chiverton
Ben Nadel at Scotch On The Rocks (SOTR) 2011 (Edinburgh) with: Tom Chiverton

Creating A StructPop() Function In ColdFusion

By
Published in

It's amazing what kind of problems you can solve with a handful of simple data structures. But, the elegance of those algorithms is, in part, constrained by the elegance of the underlying data structure APIs. For example, the Array.pop() and Array.shift() methods — which simultaneously read-from and delete-from the given array — are extremely useful and bring a high degree of readability to your algorithms. And although ColdFusion finally brought pop() and shift() style methods to the language a few versions ago for arrays, there's no equivalent for structs. As such, I wanted to look at what a Struct.pop() method might look like.

The idea here is the same, the Struct.pop() method would read-from and delete-from the given struct. The read operation would return the value stored at the given key; then, the delete operation would delete the keyed entry. And since ColdFusion structs are passed by reference, this in-place mutation is mirrored in the calling context.

Here's a quick demo. We're going to define a struct, pop two keys, then dump the struct demonstrating that the two popped keys are no longer present:

<cfscript>

	// ColdFusion language extensions (global functions).
	include "/core/cfmlx.cfm";

	// ------------------------------------------------------------------------------- //
	// ------------------------------------------------------------------------------- //

	queue = [:];
	queue.one = "this is one";
	queue.two = "this is two";
	queue.three = nullValue();
	queue.four = "this is four";

	dump([
		// Output the taken values.
		"Value One": structPop( queue, "one" ),
		"Value Three": structPop( queue, "three" ),

		// Output the mutated-in-place struct.
		"Source Struct": queue
	]);

	// ------------------------------------------------------------------------------- //
	// ------------------------------------------------------------------------------- //

	/**
	* I return the value stored at the given key, then delete the keyed entry from the
	* source struct.
	*/
	public any function structPop(
		required struct source,
		required string key
		) {

		try {

			return source?[ key ];

		// In this case, `finally` isn't about error recovery, it's about cleanup. The
		// return expression is evaluated before the `finally` block executes, allowing us
		// to delete the keyed entry after the evaluation BUT BEFORE the value is returned
		// to the calling context. This is an unusual technique, but for me it just reads
		// SO MUCH BETTER than storing an intermediary variable and dancing around null
		// reference errors.
		} finally {

			source.delete( key );

		}

	}

</cfscript>

When we run this Adobe ColdFusion 2025 code, we get the following output:

Screenshot of the CFDump of the given popped values and source struct.

Both popped keys — one and three — have been removed from the source struct. And even through key three referenced an undefined value, the structPop() function uses the safe-navigation operator to safely propagate that undefined value to the calling context.

Note: I believe that the safe-navigation operator only started working with bracket-notation in recent years. If you're on an older version of ACF, this may very well throw a compilation error.

Popping values is an action more often associated with arrays, to be sure. But, I've absolutely authored a few algorithms where a mechanic like this would be helpful for structs.

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

Reader Comments

Post A Comment — I'd Love To Hear From You!

Post a Comment

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