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:
Launch code in new window » Download code as text file »
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:
Launch code in new window » Download code as text file »
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:
Launch code in new window » Download code as text file »
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:
| | | | ||
| | ![]() | | ||
| | | |
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:
Launch code in new window » Download code as text file »
Passing in our array to this function:
Launch code in new window » Download code as text file »
This gives us the following array object:
| | | | ||
| | ![]() | | ||
| | | |
Ok, so we see that the object is in fact passed by reference. Now, let's test the access methods:
Launch code in new window » Download code as text file »
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:
Launch code in new window » Download code as text file »
... 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.
Download Code Snippet ZIP File
Comments (2) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
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.
Posted by Daniel Roberts on Feb 7, 2007 at 10:23 AM
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.
Posted by Ben Nadel on Feb 7, 2007 at 6:58 PM