Implementing std::pair in Actionscript

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.

Example usage:

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.