Learning ColdFusion 9: Ternary Operator Works Around Implicit Array Bug

Posted July 14, 2009 at 9:57 AM by Ben Nadel

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:

  • <!--- 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:

  • <!--- 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.


You Might Also Be Interested In:



Reader Comments

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

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


Jul 14, 2009 at 10:54 AM // reply »
11,238 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 A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 21, 2013 at 7:46 PM
Using Plupload For Drag & Drop File Uploads In ColdFusion
No luck. At least I have uncovered the cause, URLScan 3.1. Here is what I see in the IIS log when a file is over 30mb. 2013-05-21 23:29:05 10.105.45.128 GET /plupload/assets/jquery/jquery-1.8. ... read »
May 21, 2013 at 6:12 PM
Using Plupload For Drag & Drop File Uploads In ColdFusion
Ben, I did not see you after Pete Freitag's Lockdown session at cfObjective but he said that IIS sets file size limits at 30MB by default which just happened to be the threshold for file size when ... read »
May 21, 2013 at 11:51 AM
Ask Ben: Parsing Very Large XML Documents In ColdFusion
Looking at my first ever XML document that I have to parse and put into MS SQL 2000 with CF8. I get it to list the desired Field name, many times over, and have a long list of this field name displa ... read »
May 21, 2013 at 9:25 AM
Turning Off and On Identity Column in SQL Server
you are awesome..i am lucky to get this blog between such a garbage one....Thanks, Prashant ... read »
May 20, 2013 at 4:38 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, Your confusion is well founded, since this is a very confusing features. In fact, it ONLY works if you use array notation. Meaning, that this: arrayToList( query[ "columnName" ] ) ... read »
May 20, 2013 at 4:34 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I was thinking chicken and the egg, I wouldn't have expected it to work in the valuelist going in I guess. Maybe I just need a beer, long day :) ... read »
May 20, 2013 at 4:29 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, That's if you're trying to reference a specific row. In this case, we're trying to reference the entire query column as one cohesive value. So, you are correct that if you wanted to output a ... read »
May 20, 2013 at 4:24 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I thought when you used array notation to reference queries you always had to have the row or it would throw a similar error as well? ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools