ColdFusion Arguments Object Can Act As Ghetto Pass-By-Reference Array

Posted February 6, 2007 at 11:30 PM by Ben Nadel

Tags: ColdFusion

As you may or may not know, the ARGUMENTS object that holds the parameters passed to a ColdFusion function can be accessed as either a key-value structure or as an indexed array:

  • <cffunction
  • name="Massage"
  • access="public">
  •  
  • <!--- Define arguments. --->
  • <cfargument name="Girl" type="any" required="true" />
  •  
  • <!--- Get girl via arguments STRUCT. --->
  • <cfset ARGUMENTS.Girl />
  •  
  • <!--- Get girl via arguments ARRAY. --->
  • <cfset ARGUMENTS[ 1 ] />
  •  
  • <cfreturn />
  • </cffunction>

Notice that the "Girl" argument can be accessed as a key or an index. This got me thinking about the ARGUMENTS object. What is it? How does it act? Well, let's start with what it is; when you grab it's underlying Java class and the super class chain, you get this:

1. coldfusion.runtime.ArgumentCollection
2. coldfusion.util.FastHashtable
3. java.util.Hashtable

It's some sort of ArgumentCollection that extends the HashTable (think ColdFusion Struct). Now, as you know, Structures in ColdFusion are passed by reference, not by value. But, they can also be used as Arrays. Does this mean that we could potentially use the ARGUMENTS collection as a pass-by-reference array in ColdFusion?

Before even testing this, we have to figure out how to get an ARGUMENTS collection. You can try to create it using CreateObject(), but you will find that it take two parameters. There are two constructors listed and I could not figure out what either of them were supposed to take.

The easiest way to get an empty ARGUMENTS object is just have an intermediary ColdFusion function return it's own copy:

  • <!---
  • This function does nothing but reflect it's own ARGUMENTS object.
  • This will allow us to create empty ARGUMENTS objects without
  • having to know about initialization parameters.
  • --->
  • <cffunction
  • name="GetArguments"
  • access="public"
  • returntype="any"
  • output="false"
  • hint="Returns the arguments object.">
  •  
  • <cfreturn ARGUMENTS />
  • </cffunction>

Ok, now once we have a way to get a new ARGUMENTS object, we can start to test the functionality. I am guessing that access methods are going to be safer than modification methods, so let's test those first:

  • <!--- Get an array with defaulted values. --->
  • <cfset arrArguments = GetArguments( 1, 2, 3, 4, 5 ) />

Notice that since our GetArguments() method just returns the array, it allows us to set default array values. This is what this object looks like:


 
 
 

 
CFDump Arguments Object  
 
 
 

Before we go any further, we have to check to see if this object IS even passed by reference. To test this, let's create a function that modifies a passed-in array but does NOT return it:

  • <cffunction
  • name="AlterArray"
  • access="public"
  • returntype="void"
  • output="false"
  • hint="Alter an array by setting first index.">
  •  
  • <!--- Alter first index of first argument. --->
  • <cfset ARGUMENTS[ 1 ][ 1 ] = "999" />
  •  
  • <!--- Return out (but do NOT return array). --->
  • <cfreturn />
  • </cffunction>

Passing in our array to this function:

  • <!--- Try to alter array by passing by reference. --->
  • <cfset AlterArray( arrArguments ) />
  •  
  • <!--- Dump out original array. --->
  • <cfdump
  • var="#arrArguments#"
  • label="ARGUMENTS"
  • />

This gives us the following array object:


 
 
 

 
CFDump Arguments Object  
 
 
 

Ok, so we see that the object is in fact passed by reference. Now, let's test the access methods:

  • ArrayAvg() : #ArrayAvg( arrArguments )#<br />
  • ArrayIsEmpty() : #ArrayIsEmpty( arrArguments )#<br />
  • ArrayLen() : #ArrayLen( arrArguments )#<br />
  • ArrayMax() : #ArrayMax( arrArguments )#<br />
  • ArrayMin() : #ArrayMin( arrArguments )#<br />
  • ArraySum() : #ArraySum( arrArguments )#<br />
  • ArrayToList() : #ArrayToList( arrArguments )#<br />
  • IsArray() : #IsArray( arrArguments)#<br />

This gives us the following output:

ArrayAvg() : 3
ArrayIsEmpty() : NO
ArrayLen() : 5
ArrayMax() : 5
ArrayMin() : 1
ArraySum() : 15
ArrayToList() : 1,2,3,4,5
IsArray() : NO

As you can see, everything but the IsArray() method acts just like we would think it would.

I would demonstrate the array modification methods:

  • ArrayInsertAt() : #ArrayInsertAt( arrArguments, 1, 5 )#<br />
  • ArrayClear() : #ArrayClear( arrArguments )#<br />
  • ArrayInsertAt() : #ArrayInsertAt( arrArguments, 1, 5 )#<br />
  • ArrayPrepend() : #ArrayPrepend( arrArguments, 6 )#<br />
  • ArrayResize() : #ArrayResize( arrArguments, 2 )#<br />
  • ArraySet() : #ArraySet( arrArguments, 2, 10, 1000 )#<br />
  • ArraySort() : #ArraySort( arrArguments, "textnocase" )#<br />
  • ArraySwap() : #ArraySwap( arrArguments, 1, 2 )#<br />

... but, MOST OF THESE CAUSED PROBLEMS! It was actually really disappointing :( So, bottom line is, you can use the ARGUMENTS object as a read-only, pass-by-reference array, but it doesn't quite work. And, if you are gonna use it as a read-only array, you might as well just create a Java ArrayList which is passed by reference anyway and is not as ghetto.




Reader Comments

Feb 7, 2007 at 10:23 AM // reply »
27 Comments

This reminds me of a problem I ran into a while back. I was trying to get some code working for grabbing info from some websites. I had to get cookies working properly and the documentation for cfhttp said cookies are returned in an array. I had random problems and I think it worked at one point but in later versions of CF failed. Anyways, it turned out that the variable was actually a struct with numbered keys, not an array, which caused problems of course.

Now thats out of the way... What were you hoping to accomplish with this? Did you have some particular uses in mind? I know I've run into situations where pass by reference could be useful but nothing springs to mind other than maybe putting a reference into a different scope.


Feb 7, 2007 at 6:58 PM // reply »
11,246 Comments

Daniel,

I had no goal to accomplish here. I was on my way home the other day and suddenly I realized that I had no idea "what" the ARGUMENTS scope even was. I knew it could be accessed as both an array and as a struct, so, it couldn't be either of them at the same time.

I just wanted to play, see what would happen.


Sep 4, 2011 at 1:31 PM // reply »
1 Comments

Dear Ben,

Your website is an incredible resource - you really need to get a job at Adobe as a product evangelist! You seem to have much more insight into the language than anybody else I have come across.

I have one *minor* problem - you spell "its" wrongly several times in this posting. "It's" means "it is" or "it has", not "of it".

e.g. "ColdFusion function return it's own copy"

Should read: "ColdFusion function return its own copy".

Thanks again for your blog!

With best wishes,

Mark Winterton


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 23, 2013 at 9:52 PM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Muhmmadibn Did you figure out a solution to launching PDFs? I am running into the same issues myself. There is no way to close the PDF or go back once you launch it. Thanks in advance! ... read »
May 23, 2013 at 6:06 PM
The Girl Who Broke My Heart, And Made Me A Better Person
Good day,ladies and gentle men, my name is Dr AMADI the great spell caster in Africa, i have help so many people for different kind of problems,who say there is no solution to problems on earth, that ... read »
May 23, 2013 at 4:26 PM
ColdFusion QueryAppend( qOne, qTwo )
@Heather, Glad people are still getting value out of this! ... read »
May 23, 2013 at 3:49 PM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, I meant the code at the bottom (not the video). I did try to experiment with an intermediary variable, like: value = users.id[ i ]; arrayContains( userIDs, value ); ... but t ... read »
May 23, 2013 at 11:06 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Are you talking about As Number: YES As String: YES As Java: YES? If so, that's with 3 different ways of referencing the constant 1, not users.id[1]. Query object references(*) are what seem ... read »
May 23, 2013 at 9:55 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Dan, According to the CF Admin, I'm running Java "1.6.0_45". As far as the DB column, in the database it's an INT. I'll see if I can dig into what CF sees it as. @WebManWalking, But h ... read »
May 23, 2013 at 9:49 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, I think the problem is that we're used to loose typing in ColdFusion, like JavaScript. If a value is a number but it's needed in an expression to be a string, noooo problem. I've encountered ... read »
May 23, 2013 at 9:47 AM
ColdFusion QueryAppend( qOne, qTwo )
You rock! Thank you, thank you, thank you!!! ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools