The random utterances of David Arno

collections.immutable: Immutable collection classes for AS3

As part of writing a preprocessor for generating AS3 enums, I hit upon the need for a means of passing around a set of enums in a way that couldn’t corrupt the original data. After a bit of thought and digging around the core Flash/ Flex classes, I realised that:

  1. There are no immutable collections in Flash or Flex.
  2. Immutable collections are  fairly easy to create using standard AS3 and the magical Proxy class.


Lets take a step back and consider what I mean by an immutable collection. To demonstrate this, consider an arbitrary class Wibble:

class Wibble
{
  public var wobble:Boolean = true;
}

If we have an array of Wibble objects:

var array:Array = [new Wibble(), new Wibble(), new Wibble()]

we are free to manipulate that array as we wish:

array[1].wobble = false;
array[0] = 0;
array = null;
array = [];

The last three actions change the array. The first one changes the state of one of the values of the array. This distinction is important, for if Array were immutable collection, only the first action would be valid. The values of an immutable collection cannot be changed, but the state of those values can be.

With that in mind, I have created a small suite of immutable collection classes:

HashMap An immutable version of the Dictionary class
OrderedList An immutable version of the Array class
Tuple A variation on OrderedList, designed for handling situations where functions need to return a set of loosely connected values
TypedHashMap Variation on HashMap, which allows the type of the map’s values to be specified
TypedOrderedList Variation on OrderedList, which allows the type of the elements of the list to be specified

The API documentation is available online here. Don’t forget you can use “Doc? AIR” to create an offline version if that suits you better.

The SWC library and source can be downloaded here.

Tutorial posts on each of the classes will appear here soon.


Share This Post...
3 comments so far, click here to read them or add another

3 Comments so far

  1. Rudiger February 24th, 2011 22:20

    Can you use these with data binding?

  2. David Arno March 7th, 2011 13:21

    @Rudiger,

    Why would you ant to use data binding with these classes. Data binding is used to bind a component to a changeably piece of data. These collection classes are immutable, ie they do not change. So binding to them would have no effect.

  3. Alex.G September 29th, 2011 15:38

    I’m not sure why everyone is calling their dictionary wrappers a “HashMap” when they don’t use any hash and are still just IdentityMaps. It’s missleading. So far I’ve only seen ONE real AS3 hashmap, out of 10+.

Leave a reply

Close