Friday, April 30, 2010

Introducing: Better-AS3-Collections!

Anyone programming in Adobe Flex using ActionScript3 has probably felt the need for a more featureful collections library:
  1. The Flex built-in primitive collection types (Array, Object, and XML) are not related to each other in a useful way (Array extends Object, but Array is not an associative array). 
  2. The built-in collection operations don't cover enough common use-cases.
Unlike some other attempts, better-as3-collections is not a port of the java.util.collections package. In fact, it doesn't set up a type hierarchy at all!

Why not?
  • Interoperability. If you have existing code that uses Arrays and Objects, then moving to a new type hierarchy will by necessity involve a lot of casting and new Objects. Indeed, if you want to make use of Flex's GUI types, using Array is almost a requirement.
  • Sugar. If your new collections framework doesn't inherit from Array or Object, you won't be able to use bracket syntax to retrieve items from or set items into the collection. 
Rather, it extends the functional style hinted at by Adobe's own Array: see the map() and filter() functions. My implementation draws heavy inspiration from the Ruby Hash and Array types. In spirit it's very related to this post by Bruce Eckel, who talks about extending the Flex collections types with more functional-style methods.

A more concrete introduction can be found on the project site: http://code.google.com/p/better-as3-collections/ .

Feel free to check out the unit tests for example uses, or just check out the source!

1 comment: