This post is pretty out of date, and I wouldn’t use the same solution today. I’m leaving it here for posterity, but don’t consider it some kind of expert advice.
One of the things that bothers me about Actionscript is the lack of templates. The Vector class is their only appearance, and since Adobe hasn’t seen fit to open up the feature for general use it looks like that won’t be changing anytime soon. I’m not very good at using templates in my own C++ classes, but I certainly like to use them as part of the STL.
This week I needed a function to return two values at once. “No problem,” thought I. “I’ll just use a std::pair to store both.” A few seconds later I realized that I was still thinking in C++, so I went around searching for a comparable class in Actionscript, only to find that none existed. This was not to be borne.
Therefore I present a class I wrote to fill the gap. It’s compiler safe and mirrors the design of std::pair as much as possible.
// Create a Pair and initialize it to store a uint and a string.
var Foo:Pair = new Pair(uint, String);
// Store the values
Foo.first = 40;
Foo.second = "Testing!";
// Create variables from Foo's data
var someUint:uint = Foo.first;
var someString:String = Foo.second;
// Create a new Pair from an existing variable with the static function "copy"
var Quux:Pair = Pair.copy(Foo);
I haven’t done a huge amount of testing, but I’m happy with how it works so far. As usual it’s open-source to do with what you want. The repository is here.