Strange Numericly Named CFInvokeArgument Behavior

Posted March 29, 2007 at 2:33 PM by Ben Nadel

Tags: ColdFusion

A while back, I was talking about Sean Corfield and closures and I had asked him how to send an unknown number of "nameless" arguments to a function. He had suggested just sending them over named, but using the argument index as their name. I thought this was a brilliant idea and tested. It works great, but in the middle of testing, I had a typeo that caused some whacky results.

Look what happens when I send over index-named arguments where the index-number does not line up with the actual argument index:

  • <cffunction
  • name="DumpArgs"
  • access="public"
  • returntype="void"
  • output="true"
  • hint="Does nothing but CFDump out the arguments.">
  •  
  • <!--- Dump out arguments. --->
  • <cfdump
  • var="#ARGUMENTS#"
  • label="ARGUMENTS Scope"
  • />
  •  
  • <!--- Return out. --->
  • <cfreturn />
  • </cffunction>
  •  
  •  
  • <!--- Invoke DumpArgs sending over index-named arguments. --->
  • <cfinvoke method="DumpArgs">
  • <cfinvokeargument name="1" value="Kim" />
  • <cfinvokeargument name="5" value="Jessie" />
  • <cfinvokeargument name="10" value="Samantha" />
  • </cfinvoke>
  •  
  • <cfinvoke method="DumpArgs">
  • <cfinvokeargument name="10" value="Samantha" />
  • <cfinvokeargument name="5" value="Jessie" />
  • <cfinvokeargument name="1" value="Kim" />
  • </cfinvoke>
  •  
  • <cfinvoke method="DumpArgs">
  • <cfinvokeargument name="1" value="Kim" />
  • <cfinvokeargument name="10" value="Samantha" />
  • <cfinvokeargument name="5" value="Jessie" />
  • </cfinvoke>

Notice that in the three different method invokes, we send the arguments in completely different orders. This gives us the following CFDumps:


 
 
 

 
CFInvokeArgument Arguments CFDump 1  
 
 
 

 
 
 

 
CFInvokeArgument Arguments CFDump 2  
 
 
 

 
 
 

 
CFInvokeArgument Arguments CFDump 3  
 
 
 

Notice that the "first" element seems to be ordering somewhat alphabetically based on the relationship of 1 and 10. I can't quite see the pattern. But then the second and third elements are always undefined struct elements. I wonder why it is not trying to order those in some sort of alpha way. Very confusing.




Reader Comments

Mar 29, 2007 at 6:04 PM // reply »
4 Comments

Ah. I was quite befuddled myself, but couldn't quit until I figured it out. The cause for this behaviour is a combination of CF's universal variant datatype, and your use of syntactically invalid variable names.

CF variable names have to start with an alpha, underscore, or currency character. I'm not sure why, but the CF compiler apparently doesn't validate your variable names when you pass them in via cfinvokeargument (probably because it assumes you're calling named arguments that exist and have been validated within the function). So when the the arguments are passed in, it first creates an Arguments Array with three elements, and then tries to populate an Arguments Struct with the elements in the array: "1", "5" and "10", but when it tries to populate that struct, it calls Arguments["1"], but it can't tell the difference between that and Arguments[1], because internally, it's a variant either way. As such, Arguments["1"] returns the first element in the Arguments array, and Arguments["10"] is trying to return the 10th element in the Arguments array, which obviously doesn't exist.

If you change your <cfinvokeargument> tags to pass in "1", "2", and "3" instead, you'll find that all Argument structure values are populated, but in the order you passed them in, not according to the keys you specified.

Likewise, if you pass them in as "one", "ten" and "five" they will act as expected.


Mar 29, 2007 at 6:12 PM // reply »
4 Comments

Another possible solution to your quandry would be to pass them in in array syntax, e.g.

<cfinvoke method="DumpArgs">
<cfinvokeargument name="args[3]" value="Kim" />
<cfinvokeargument name="args[2]" value="Jessie" />
<cfinvokeargument name="args[1]" value="Samantha" />
</cfinvoke>

Don't know if that fits what you're trying to do, but it seems to get around the immediate problem.


Mar 30, 2007 at 4:10 AM // reply »
26 Comments

Hi,

actually you can pass argument names that are not syntactically valid variable names.

<cfinvokeargument name="another girl" value="Kim" />

will work as expected. And it should, too, because all strings can be keys of a struct. myVar['some invalid var name'] is a syntactically correct way to address a struct element (as is myVar['10'], if myVar is a struct and not an array). If numbers don't work with <cfinvokeargument>, I think it should be considered a bug.

However, if you want to pass in arguments without a name and address them in the order they were passed in, you can use the script syntax:

<cfoutput>#DumpArgs('Kim','Jessie','Samantha')#</cfoutput>

Chris


Mar 30, 2007 at 7:07 AM // reply »
11,238 Comments

@Chris,

The only problem with calling DumpArgs() in that fashion is that I needed to have logic in which arguments were sent:

DumpArgs(
if (true){ 'Samantha', }
if (false){ 'Kim', }
if (true){ 'Nicki', }
);

Now, obviously, that is NOT working code. I was just showing you what I had in my mind. This type of logic can be accomplished using the CFInvoke and CFInovkeArgument tags as you can have CFIF statements in them.... I just didn't know how to label the "name" attribute of the argument tag. The index-based naming will take care of that now.... I just need an elegant way to increment the index number as a I use it.


Mar 30, 2007 at 7:54 AM // reply »
26 Comments

@Ben,

Ahhh... I see.

Couldn't you simply use an array, conditionally append element, and then pass that array to the function?

Or, if you need single arguments within your function, could you use a struct with numeric keys and pass that struct in as argumentcollection?

Chris


Mar 30, 2007 at 8:00 AM // reply »
11,238 Comments

@Chris,

I really like the Struct with numeric keys and then an argumentCollection... Very slick idea, you old dog! I will give that a shot.


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 19, 2013 at 2:31 PM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
It's funny really just how well that image describes the way I would imagine most people that go with angular for some project is. I have had a similar roller-coaster ride with it as well, but not qu ... read »
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 10:37 PM
Very Simple Pusher And ColdFusion Powered Chat
hi id making plz easy ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools