Skip to main content
Ben Nadel at cf.Objective() 2009 (Minneapolis, MN) with: Ryan Vikander and Kashif Khan and John Hiner
Ben Nadel at cf.Objective() 2009 (Minneapolis, MN) with: Ryan Vikander Kashif Khan John Hiner

I Can Finally Embed An Assignment Operation Inside Of Other Expressions In Lucee 5.3.2.77

By
Published in Comments (4)

CAUTION: This feature is actually buggy in Lucee. It works within the the return context of this post; but, it seems to break everywhere else. See Comments section below for more information.


Having recently switched from Adobe ColdFusion 10 to Lucee CFML 5.2, I've been digging through all of the new CFML features that I can take advantage of. And, while it may seem silly, one feature that I've been wanting for years is the ability to perform an assignment operation inside of other expressions. To this day, Adobe ColdFusion throws a syntax error when I try to do this. But, Lucee CFML lets me do it, no problem! And, I bet the Lucee product team would argue that this isn't even a "feature" - that it's just "correct syntax support". But, I don't care - I can do it, and I'm loving it!

To see what I mean, let's take a look at this super simple demo. I have a transform() function that transforms an input value into an output value. And, as part of the transformation logic, the function attempts to pull from a cache. Look at the last line of the function: I am transforming the value, pushing it into the cache (the assignment operation), and providing it as the return payload - all at the same time:

<cfscript>

	valueCache = {};

	/**
	* I transform the given value (it's a silly demo).
	* 
	* @input I the value being transformed.
	* @output false
	*/
	public string function transform( required string input ) {

		if ( structKeyExists( valueCache, input ) ) {

			return( valueCache[ input ] );

		}

		// NOTE: This return statement is ALSO an ASSIGNMENT OPERATION! Woot woot!!
		return( valueCache[ input ] = ucase( input ) );

	}

	echo( transform( "I rock the party that rocks the body!" ) & "<br />" );
	echo( transform( "You rock the party that rocks the body!" ) & "<br />" );
	echo( transform( "I rock the party that rocks the body!" ) & "<br />" );
	echo( transform( "You rock the party that rocks the body!" ) & "<br />" );

</cfscript>

ASIDE: Since I am running this demo in both Adobe ColdFusion and Lucee CFML, I needed only one thing break. If this were a Lucee CFML only page, I could have just used cachedWithin to apply caching to the transform() function.

As you can see, the return statement contains an embedded assignment operation. Now, if I run this code in the Lucee CFML 5.3.2.77 engine, I get the expected output:

I ROCK THE PARTY THAT ROCKS THE BODY!
YOU ROCK THE PARTY THAT ROCKS THE BODY!
I ROCK THE PARTY THAT ROCKS THE BODY!
YOU ROCK THE PARTY THAT ROCKS THE BODY!

However, if I run this same code in Adobe ColdFusion 2018, I get the following error output:

Adobe ColdFusion 2018 won't allow for assignment operations inside of other expressions.

As you can see, Adobe ColdFusion 2018 throws a syntax error when I try to perform the assignment operation inside of another expression. This has been the case for years. Getting around this issue is trivial - I just have to break the assignment operation onto its own line of code. But, to be clear, this is a work-around. And, it's one that I no longer have to do in Lucee CFML!

This isn't a "feature" of Lucee CFML, per say. This is really just the app engine "getting out of my way". But, it's been a tiny point of friction for me for years! And, I'm disproportionately excited that I no longer have to worry about it.

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

Reader Comments

449 Comments

It's weird. Never in my 15+ years of writing ColdFusion, have I ever tried to return an embedded assignment operator expression.

But, it's great to know that I can, and, as you pointed out, this should have been part of the core language, anyway!

Onwards & upwards...??

15,902 Comments

@Charles,

Ha ha, to be clear, this isn't just for return statements. Another common place that I use this in JavaScript (at least) is in things like a while() loop:

while ( value = someFunction() ) {
	// ....
}

Here, there is an assignment inside the while() condition expression. This also works in Lucee (but not in Adobe ColdFusion). Though, this particular one is less useful since Lucee doesn't have the same breadth of Truthy / Falsey values that JavaScript has.

But, the point is, certain types of algorithms can be slightly simplified with this assignment / expression support :D

15,902 Comments

@All,

Ok, so it turns out that this approach - using assignment within a parent expression - is actually buggy in Lucee. While it happens to work inside of a return statement, it does not seem to work elsewhere.

I will write this bug up in the morning and report it (assuming it doesn't already exist in the bug log).

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