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:




Reader Comments

Eric Belair
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,371 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 7, 2009 at 5:53 PM
Ask Ben: Javascript String Replace Method
You can find here an advanced function that prepared with javascript replace function. This can make the first letters of words, sentences, lines and whatever you define automatically: http://www.m ... read »
Andrew Neely
Nov 7, 2009 at 4:56 PM
A Moment That Touched Me - The Fountainhead
Ben, Glad you enjoyed the podcast. Yeah, the Tank Riot guys can get really chatty during the episodes, but that's part of the charm of it for me. They've covered everything from Nichola Tesla to Cha ... read »
Nov 7, 2009 at 4:43 PM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
Is it possible to make some more MenĂ¼`s ? ... read »
Jill
Nov 7, 2009 at 11:40 AM
How To Unformat Your Code (Like A Pro)
Derek, I think you might be right - sweet! Thanks for the link :) ... read »
Nov 7, 2009 at 11:25 AM
How To Unformat Your Code (Like A Pro)
I think it would be way easier to just use this http://www.logichammer.com/html-formatter/ He just released v3 and it rocks. ... read »
Jill
Nov 7, 2009 at 7:58 AM
How To Unformat Your Code (Like A Pro)
LMAO - this was pretty funny! I have to admit - I also love to reformat code so I can read it. My boss used to tell me to leave my OCD at home. Now I don't feel so bad after reading everyone else' ... read »
Nov 6, 2009 at 10:10 PM
How To Unformat Your Code (Like A Pro)
The timing of this post is just uncanny. I spent the last 15-20 minutes manually un-formatting my "Ben Nadel" style code within a CFC of mine. I was really digging the readability a few weeks ago, bu ... read »
Roe
Nov 6, 2009 at 5:11 PM
Passing Arrays By Reference In ColdFusion - SWEEET!
ArraySort also reorders the results of these java obj's ... read »