My Secret Life as a Spaghetti Coder
home | about | contact | privacy statement
(or scopes as structs, as it were)

So, what would you expect the output of this to be, and why?

<cfset url.test = 1>
<cfset tempurl = structcopy(url)>
<cfset structdelete(tempurl, "test")>
<cfoutput>
   #structkeyexists(url,"test")#
</cfoutput>


I thought it should be "YES", but in fact it is "NO." I'm sure I'm overlooking something - or else I'm stating something everyone already knows. I've posted to the CFCDev list - perhaps someone there can help me understand.

I'm using Macromedia's CFMX 6.1, for reference. I don't know how this behaves in 7.

Update: Sean Corfield wrote about this behavior back in 2004. It's quite a bit annoying, since CF says it is a struct, and treats it like one in just about every way you can imagine, except for this. I'm getting the same behavior using the duplicate() function as well. Guess I'll have to copy it manually.

Update 2: Actually, I said in the last post I was getting the same behavior using duplicate(), but I was wrong. I wasn't doing the exact same thing, but I was doing something I would still expect to work. Here it is:

<cfset form.test = 1>
<cfset tempform = duplicate(form)>
<cfset structdelete(form, "test")>
<cfset form=duplicate(tempform)>
<cfoutput>
In Form: #structkeyexists(form,"test")#<br/>
In Temp Form: #structkeyexists(tempform,"test")#
</cfoutput>

Still strange.

Update 3: I didn't think about it until this morning (November 1, 2006), but this is a gross violation of LSP (Read Uncle Bob's PDF on LSP). Now, I know the underlying Java classes are different, as Sean Corfield pointed out in the post I linked to above, but to me, if you return a true value from isStruct(form), then that tells me it ought to behave in every way like a structure.

Hey! Why don't you make your life easier and subscribe to the full post or short blurb RSS feed? I'm so confident you'll love my smelly pasta plate wisdom that I'm offering a no-strings-attached, lifetime money back guarantee!


Comments
Leave a comment

Does this complicate things futher?

<cfset URL.test = 1>

<cfset newStruct = StructNew()>
<cfset newStruct.putAll(URL)>

<cfdump var="#URL#" label="URL">
<cfdump var="#newStruct#" label="newStruct">

<cfset StructDelete(newStruct, "test")>

<cfdump var="#URL#" label="URL">
<cfdump var="#newStruct#" label="newStruct">

<cfdump var="#StructKeyExists(URL, 'test')#">

Probably not. But as a way to create a new structure from an old one it's not bad.

Credit to Dom for that http://fusion.dominicwatson.co.uk/2007/09/singular...

Posted by Adrian Lynch on Dec 25, 2007 at 01:27 PM UTC - 6 hrs

It doesn't complicate things since the behavior appears correct, but it sure makes it nice to have it behave as expected.

Posted by Sammy Larbi on Dec 27, 2007 at 12:51 PM UTC - 6 hrs

Leave a comment

Leave this field empty
Your Name
Email (not displayed, more info?)
Website

Comment:

Subcribe to this comment thread
Remember my details
Google
Web CodeOdor.com

Me
Picture of me

Topics
.NET (19)
AI/Machine Learning (14)
Answers To 100 Interview Questions (10)
Bioinformatics (2)
Business (1)
C and C++ (6)
cfrails (22)
ColdFusion (78)
Customer Relations (15)
Databases (3)
DRY (18)
DSLs (11)
Future Tech (5)
Games (5)
Groovy/Grails (8)
Hardware (1)
IDEs (9)
Java (38)
JavaScript (4)
Linux (2)
Lisp (1)
Mac OS (4)
Management (15)
MediaServerX (1)
Miscellany (76)
OOAD (37)
Productivity (11)
Programming (168)
Programming Quotables (9)
Rails (31)
Ruby (67)
Save Your Job (58)
scriptaGulous (4)
Software Development Process (23)
TDD (41)
TDDing xorblog (6)
Tools (5)
Web Development (8)
Windows (1)
With (1)
YAGNI (10)

Resources
Agile Manifesto & Principles
Principles Of OOD
ColdFusion
CFUnit
Ruby
Ruby on Rails
JUnit



RSS 2.0: Full Post | Short Blurb
Subscribe by email:

Delivered by FeedBurner