Learning ColdFusion 9: Ternary Operator Works Around Implicit Array Bug

Posted July 14, 2009 at 9:57 AM

Tags: ColdFusion

Earlier this week, I examined the updated implicit array and implicit struct creation functionality in ColdFusion 9. While they provide awesome functionality, I was a little disappointed to find out that there is still some sort of improper order of operations in which the left-hand variable is being defined before the right-hand expression is evaluated. This can lead to very strange and buggy behavior as in the following example:

 Launch code in new window » Download code as text file »

  • <!--- Create a raw string value. --->
  • <cfset data = "Simple Value" />
  •  
  • <!---
  • Convert the data variable to an array using implicit
  • array notation.
  • --->
  • <cfset data = [ data ] />
  •  
  • <!--- Output the new data variable. --->
  • <cfdump
  • var="#data#"
  • label="Data[ data ]"
  • />

Here, we are converting a simple variable into an array containing the original value. This should work just fine; however, due to the error in order of operations, ColdFusion produces the following data structure:

 
 
 
 
 
 
ColdFusion 9 Still Has Errors In Its Order Of Operations In Implicit Array And Struct Creation. 
 
 
 

Clearly, something went horribly wrong.

When I was digging through ColdFusion 9's new ternary operator, I found out that if you use a ternary operator to execute this data transformation, it actually executes properly:

 Launch code in new window » Download code as text file »

  • <!--- Create a raw string value. --->
  • <cfset data = "Simple Value" />
  •  
  • <!---
  • Convert the data variable to an array using a
  • ternary operator.
  • --->
  • <cfset data = (true ? [ data ] : []) />
  •  
  • <!--- Output the new data variable. --->
  • <cfdump
  • var="#data#"
  • label="Data ? [ data ]"
  • />

Here, rather than storing [ data ] directly back into the data variable, we are proxying it through the ternary operator. The "false" statement, [], is never to be used. When we run this code, we get the following CFDump:

 
 
 
 
 
 
ColdFusion 9's New Ternary Operator Can Be Used To Work Around The Implicit Array Bug. 
 
 
 

As you can see, the simple data value was successfully converted into an array containing the original, simple value. While this might seem rediculous, remember that the condition statement here would most likely be something like:

!isArray( data )

... which would make this much more natural. For a better example, see my post on ColdFusion 9's ternary operator.

This hack does not work with the implicit struct bug; you still end up getting an infinitely nested result.

Now, obviously, the implicit array solution here is not the solution that we ultimately want. Ultimately, we want the final release of ColdFusion 9 to fix the core problem in statement evaluation. But, if you need to overwrite a variable with itself (I found this bug because it's a common use case), this appears to be a reasonable work-around for the time being.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page


You Might Also Be Interested In:



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Jul 14, 2009 at 10:28 AM // reply »
17 Comments

Crayz. You must've really had to dig deep to find this one!


Jul 14, 2009 at 10:54 AM // reply »
6,516 Comments

@Eric,

Converting data types is something that happens more often than you realize, so when ternary operators came along, it felt like a natural thing to try. But, when I tried it, I was like, "wait! isn't that supposed to break?!?"


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »
Nov 21, 2009 at 10:56 AM
HostMySite.com Has The Best ColdFusion Hosting
@Mehul, Yes very nice people, however several downtimes per day which was not acceptable. Hence we had to move out. I am glad you are having good luck with them so far. ... read »