Sunday, July 26, 2026
HomeJavaScriptCreating A StructPop() Perform In ColdFusion

Creating A StructPop() Perform In ColdFusion


It is superb what sort of issues you may remedy with a handful of straightforward knowledge buildings. However, the magnificence of these algorithms is, partly, constrained by the magnificence of the underlying knowledge construction APIs. For instance, the Array.pop() and Array.shift() strategies — which concurrently read-from and delete-from the given array — are extraordinarily helpful and convey a excessive diploma of readability to your algorithms. And though ColdFusion lastly introduced pop() and shift() fashion strategies to the language a number of variations in the past for arrays, there isn’t any equal for structs. As such, I needed to have a look at what a Struct.pop() technique may appear to be.

The concept right here is identical, the Struct.pop() technique would read-from and delete-from the given struct. The learn operation would return the worth saved on the given key; then, the delete operation would delete the keyed entry. And since ColdFusion structs are handed by reference, this in-place mutation is mirrored within the calling context.

Here is a fast demo. We’ll outline a struct, pop two keys, then dump the struct demonstrating that the 2 popped keys are now not current:

<cfscript>

	// ColdFusion language extensions (international features).
	embody "/core/cfmlx.cfm";

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

	queue = [:];
	queue.one = "that is one";
	queue.two = "that is two";
	queue.three = nullValue();
	queue.4 = "that is 4";

	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 worth saved on the given key, then delete the keyed entry from the
	* supply struct.
	*/
	public any perform structPop(
		required struct supply,
		required string key
		) {

		strive {

			return supply?[ key ];

		// On this case, `lastly` is not about error restoration, it is about cleanup. The
		// return expression is evaluated earlier than the `lastly` block executes, permitting us
		// to delete the keyed entry after the analysis BUT BEFORE the worth is returned
		// to the calling context. That is an uncommon approach, however for me it simply reads
		// SO MUCH BETTER than storing an middleman variable and dancing round null
		// reference errors.
		} lastly {

			supply.delete( key );

		}

	}

</cfscript>

Once we run this Adobe ColdFusion 2025 code, we get the next output:

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

Each popped keys — one and three — have been faraway from the supply struct. And even via key three referenced an undefined worth, the structPop() perform makes use of the safe-navigation operator to securely propagate that undefined worth to the calling context.

Observe: I imagine that the safe-navigation operator solely began working with bracket-notation lately. In case you’re on an older model of ACF, this may increasingly very effectively throw a compilation error.

Popping values is an motion extra typically related to arrays, to make sure. However, I’ve completely authored a number of algorithms the place a mechanic like this is able to be useful for structs.

Wish to use code from this submit?
Take a look at the license.


https://bennadel.com/4907

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments