Comparing ColdFusion Struct Equality With Java
Posted August 21, 2006 at 6:15 PM
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
Newer Post
ColdFusion Boolean Evaluation And #( 3 + "Yes" )# = 4
Older Post
QuerySetCell(), StructInsert(), And Other Silly ColdFusion Functions
Reader 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"!
@Tony,
Hmm, very interesting. I never ended up using this anywhere; but it's good to know about these issues. Thanks.
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!




