Skip to main content
Ben Nadel at CF Summit West 2024 (Las Vegas) with: Steven Benjamin
Ben Nadel at CF Summit West 2024 (Las Vegas) with: Steven Benjamin

Calling Member Methods On Literals In Adobe ColdFusion 2025.0.8

By
Published in Comments (2)

As of Adobe ColdFusion 2025 update 8, you can now call member methods directly on struct, array, and string literals. You can also use bracket access on the result of a method call. Historically, you've had to use an intermediary variable assignment. But now, you can chain these operations directly onto the end of a raw literal. To be honest, this doesn't come up very often for me; but, it's come up enough for me to want to explore it.

Caution: you cannot call member methods on numbers. I believe this is true of many languages because it creates a parsing issue. The parser can't tell if the . denotes a decimal point or a method call. And while JavaScript allows you to wrap a number in parenthesis, (3).toFixed(2), attempting to do so in ColdFusion throws a parsing error.

Here's a quick demo that builds a collection of friends and then sorts them. I'm calling .insert() directly on {} literals, I'm calling .append() directly on [] literals, and I'm using [1] bracket access directly on a method call.

In fairness, I would never use such verbose construction in real life — I'm only taking the scenic route here for testing purposes:

<cfscript>

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

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

	// Member method calls directly on Struct literals.
	steve = {}
		.insert( "id", 1 )
		.insert( "name", "Steve" )
		.insert( "coolness", 0.3 )
	;
	laura = {}
		.insert( "id", 2 )
		.insert( "name", "Laura" )
		.insert( "coolness", 0.87 )
	;
	kit = {}
		.insert( "id", 3 )
		.insert( "name", "Kit" )
		.insert( "coolness", 0.54 )
	;

	// Member method calls directly on Array literals.
	friends = []
		.append( steve )
		.append( laura )
		.append( kit )
	;

	// Bracket-access directly on result of method call.
	coolestFriend = friends
		.sort( coolnessComparator( "desc" ) )
		[ 1 ]
	;
	lamestFriend = friends[ -1 ];

	dump( friends );
	dump( coolestFriend );
	dump( lamestFriend );

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

	/**
	* I return a coolness sort operator using the given direction. 
	*/
	private function function coolnessComparator( required string direction ) {

		return ( direction == "asc" )
			? ( a, b ) => ( a.coolness - b.coolness )
			: ( a, b ) => ( b.coolness - a.coolness )
		;

	}

</cfscript>

If we run this ColdFusion 2025.0.8 code, we get the following output without error:

Screenshot of browser showing CFDump of three variables.

The above syntax might seem a little far-fetched (ie, why would I construct objects like that — even the use of [1] I'd probably replace with .first() just for aesthetic reasons). Something a bit more practical is calling .right() on a string literal in order to left-pad it with zeros:

<cfscript>

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

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

	for ( i = 1 ; i <= 100 ; i++ ) {

		// Pad to three digits.
		echo( "000#i#".right( 3 ) );

	}

</cfscript>

This is a fairly minor update. But I appreciate that this update, along with the BIF-as-callback update, is moving the language in more of a unified direction. Adobe is slowly removing edge-cases and is started to provide unified access to the core functionality, regardless of the shape and / or origin of the values in question.

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