Comparing ColdFusion Struct Equality With Java

Posted August 21, 2006 at 6:15 PM

Tags: ColdFusion

I am really loving this whole Java thing. Have you ever wanted to compare two different Structs in ColdFusion? Maybe do something like:

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

  • <!--- Set up one struct. --->
  • <cfset objGirlA = StructNew() />
  • <cfset objGirlA.Name = "Sarah" />
  • <cfset objGirlA.IsHottie = "Yes" />
  • <cfset objGirlA.Measurements = ListToArray( "36,28,36" ) />
  •  
  • <!--- Set up another struct. --->
  • <cfset objGirlB = StructNew() />
  • <cfset objGirlB.Name = "Sarah" />
  • <cfset objGirlB.IsHottie = "Yes" />
  • <cfset objGirlB.Measurements = ListToArray( "36,28,36" ) />
  •  
  • <!--- Test for equality. --->
  • <cfset blnIsEqual = (objGirlA EQ objGirlB) />

If you ever have, then you know this throws the following error:

Complex object types cannot be converted to simple values. The expression has requested a variable or an intermediate expression result as a simple value, however, the result cannot be converted to a simple value. Simple values are strings, numbers, boolean values, and date/time values. Queries, arrays, and COM objects are examples of complex values.

No such luck. However, if you grab the Java methods on ColdFusion structs, such a thing is indeed possible. Structs have the Java method, Equals():

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

  • <cfset blnIsEqual = objGirlA.Equals( objGirlB ) />

This compares the passed struct, objGirlB, to the calling struct, objGirlA. And, you guessed it, it works just fine.

There are, however, some caveats. You can only compare simple values. Well, that's not exactly true; you can only compare values that are passed by value (not by reference), from what I can see. This includes numbers, strings, dates, and even arrays. In the above example, the Measurements key gets compared successfully even though it is an array and considered complex. If you have any keys that have components or Java objects, they are not compared; it doesn't throw an error, but I don't think any comparison is taking place. For instance, these two structs:

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

  • <!--- Create one with hotness 10. --->
  • <cfset objGirlA = StructNew() />
  • <cfset objGirlA.Name = "Sarah" />
  • <cfset objGirlA.Hotness = CreateObject( "component", "Girl" ).Init(
  • hotness = 10
  • ) />
  •  
  • <!--- Create one with hotness 9. --->
  • <cfset objGirlB = StructNew() />
  • <cfset objGirlB.Name = "Sarah" />
  • <cfset objGirlB.Hotness = CreateObject( "component", "Girl" ).Init(
  • hotness = 9
  • ) />

 

... when compares are evaluated as the same. The differences in hotness (10 vs. 9) does not get taken into account.

Anyway, not sure how useful this is, but sort of nifty little thing.

Download Code Snippet ZIP File

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



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

Reader Comments

May 14, 2009 at 7:07 PM // reply »
1 Comments

Be careful when using this technique. There are some strange quirks with using equals() with ColdFusion numbers. For instance:

<cfset a = structNew() />
<cfset a.n = 2 />

<cfset b = structNew() />
<cfset b.n = 2 />

<cfoutput>
#a.equals(b)#
</cfoutput>

The above outputs "YES" as expected. But if we change b.n to 1+1:

<cfset a = structNew() />
<cfset a.n = 2 />

<cfset b = structNew() />
<cfset b.n = 1 + 1 />

<cfoutput>
#a.equals(b)#
</cfoutput>

it now outputs "NO"!


May 19, 2009 at 9:35 AM // reply »
6,516 Comments

@Tony,

Hmm, very interesting. I never ended up using this anywhere; but it's good to know about these issues. Thanks.


Sep 17, 2009 at 9:14 AM // reply »
5 Comments

This is a great tip, thanks! Was already thinking about first converting the structs to wddx, but that didn't feel right. Will use this a lot, I guess!


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »
Nov 20, 2009 at 11:00 PM
Five Months Without Hungarian Notation And I'm Loving It
@Marcel, Yeah, I always err on the side of longer but more readable variable names. As for the camel casing of CF methods and the headless camel casing of custom items, I get around this by always ... read »
Nov 20, 2009 at 10:56 PM
Five Months Without Hungarian Notation And I'm Loving It
I use the following and love it: my.namespace.MyComponents.functionMethodsOrUDF() CONSTANT_VALUES_OR_PROPERTIES One thing I always try is to CamelCaseBuiltInColdFusionFunctions() so others can tell ... read »
Nov 20, 2009 at 5:38 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
Hi Ben, Great article. I've been looking around to see if ColdFusion image engine can programatically create the following "wrap around" effect: http://www.creativepro.com/article/photoshop-s-she ... read »
Nov 20, 2009 at 5:35 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Dave: I talked to Gert he suggested: <cfhttp method="get" url="http://{some cf website}" result="stuff" addtoken="yes" /> Note the addition of cfhttp attribute addtoken. That should persist y ... read »
Nov 20, 2009 at 5:23 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, Ahh, gotcha, yeah that makes sense. ... read »
Nov 20, 2009 at 5:17 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, sorry if I didn't make this clear. You can make it work like that if you want, just put <cfset session.foo = 1> (and <cfset application.foo = 1>) in your OnRequestStart() and it reve ... read »