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,246 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 24, 2013 at 11:21 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, Ha ha, let's us never speak of justifying "##" notation again :P ... read »
May 24, 2013 at 11:18 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Ah, so it was indeed how I vaguely remembered it to be: A direct assignment value = users.id[ i ] causes value to retain the sticky datatype of the query column. Although unnecessary in ... read »
May 24, 2013 at 9:11 AM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Brandon, Hi, No, I haven't been able to do that. I have just kept it as it is. ... read »
May 23, 2013 at 9:52 PM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Muhmmadibn Did you figure out a solution to launching PDFs? I am running into the same issues myself. There is no way to close the PDF or go back once you launch it. Thanks in advance! ... read »
May 23, 2013 at 6:06 PM
The Girl Who Broke My Heart, And Made Me A Better Person
Good day,ladies and gentle men, my name is Dr AMADI the great spell caster in Africa, i have help so many people for different kind of problems,who say there is no solution to problems on earth, that ... read »
May 23, 2013 at 4:26 PM
ColdFusion QueryAppend( qOne, qTwo )
@Heather, Glad people are still getting value out of this! ... read »
May 23, 2013 at 3:49 PM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, I meant the code at the bottom (not the video). I did try to experiment with an intermediary variable, like: value = users.id[ i ]; arrayContains( userIDs, value ); ... but t ... read »
May 23, 2013 at 11:06 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Are you talking about As Number: YES As String: YES As Java: YES? If so, that's with 3 different ways of referencing the constant 1, not users.id[1]. Query object references(*) are what seem ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools