(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!
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