Passing …(rest) parameters between functions

April 22nd, 2008

At some point when developing an application with ActionScript 3 you may need to pass a …(rest) parameter to a subsequent function call, and although at first this may appear to be pretty straightforward, doing so will not produce the results one might expect.

For example, consider the following method doSomething which accepts a single …rest parameter:

public function doSomething(rest) : void
{
    var n:int = rest.length;
    trace( "…rest.length = " + n );

    for (var i:int = 0; i < n; i++)
    {
         trace( rest[i] );
    }
}
doSomething("a","b","c");

// outputs:
// …rest.length = 3
// a
// b
// c

Let’s say we also have another function we need to invoke called doSomethingElse which accepts a …(rest) parameter as well:

public function doSomethingElse(rest) : void
{
    var n:int = rest.length;
    trace( "…rest.length = " + n );

    for (var i:int = 0; i < n; i++)
    {
         trace( rest[i] );
    }
}

Now suppose we need to pass the ...(rest) parameter from the doSomething function to the doSomethingElse function. The result would be as follows:

public function doSomething(rest) : void
{
    var n:int = rest.length;
    trace( "…rest.length = " + n );

    for (var i:int = 0; i < n; i++)
    {
     trace( rest[i] );
    }
    doSomethingElse( rest);
}
// doSomething outputs:
// …rest.length = 3
// a
// b
// c

// doSomethingElse outputs:
// …rest.length = 1
// a,b,c

So what went wrong? What happens is the original …rest parameter (passed to doSomething) is now being passed as a single parameter of type Array to the subsequent function (doSomethingElse), not as an Array of individual arguments as you may have expected.

This can easily be resolved by using Function.apply(); as can be seen in the following:

public function doSomething(rest) : void
{
    var n:int = rest.length;
    trace( "…rest.length = " + n );

    for (var i:int = 0; i < n; i++)
    {
     trace( rest[i] );
    }
    doSomethingElse.apply( null, rest);
}
// doSomething outputs:
// …rest.length = 3
// a
// b
// c

// doSomethingElse outputs:
// …rest.length = 3
// a
// b
// c

What we are doing is applying the call to the doSomethingElse function with the original rest parameter as if we were invoking the function directly. So keep this in mind should you ever need to pass a …rest parameter to a subsequent function.

IExpense Online (IEO)

April 16th, 2008

With Income Tax Returns approaching, now is as good a time as ever for me to blog about IExpense Online (IEO).

IExpense Online is the creation of my friend and co-worker Michal Glowacki. It is one of those Flex apps that really showcases what can be accomplished in Adobe Flex with a little creativity and dedication.

Built entirely in Flex, Cairngorm, PHP and MySQL, IExpense is a Free Tool which allows users to intuitively and intelligently manage their expenses and make sound budgeting decisions. You can try it out by logging in as a guest or creating a free account.

So make the most of your tax returns and check out IExpense Online (IEO).

IExpenseOnline

Web-Based UML Sequence Diagram Generator

April 14th, 2008

If you need to create sequence diagrams quickly and do not have the time to use the more traditional Software Modeling tools; Together, Enterprise Architect, Visio etc. you should take a look at www.websequencediagrams.com.

This handy little tool is pretty capable for a free web based utility and is very easy to use. It took me just seconds to create the simple sequence diagram below…

Web-Based UML Sequence Diagram Generator

So the next time you need to create UML sequence diagrams in a hurry make sure to check out this very useful tool.

Collections API update

April 2nd, 2008

I recently made a number of updates to my collections API which include some minor changes to the existing code as well as a few additional classes and interfaces which have been added.

The most significant updates involve three additional operations which have been added to the IMap interface which are as follows:

  • putAll(table:Dictionary) : void;
    Allows an Object or Dictionary instance of key / value pairs to be added to an IMap implementation.
  • putEntry(entry:IHashMapEntry) : void;
    Serves as a pseudo-overloaded implementation of the put(key:*, value:*); method in order to allow Strongly typed key / value implementations to be added to a map.
  • getEntries() : IList;
    Returns an IList of HashMapEntry objects based on all key/value pairs
  • I have also added two new additional IMap implementations to the collections API; LocalPersistenceMap, which can be utilized to provide an IMap implementation into a local SharedObject and ResourceMap which allows developers to work with a ResourceBundles via an IMap implementation. All IMap implementations; HashMap, ResourceMap and LocalPersistenceMap have been updated to implement the new operations.

    The IIterator interface has been renamed to Iterator. In addition the remove() operation has been omitted in order to enforce that a concrete implementations can not modify an aggregate. Additionally, ICollectionViewSortHelper has been renamed to CollectionSortUtil.

    Some design decisions worth mentioning involve the inclusion of multiple IMap implementations; HashMap, ResouceMap and LocalPersistanceMap. Initially I identified an AbstractMap from which these classes would extend, however I realize some developers may want to minimize dependencies as much as possible therefore I decided to simply have each concrete map implement the IMap interface rather than extend an abstract map implementation.

    The source, binary and ASDocs for the new Collections API can be downloaded here.

    The Collections API is published under the MIT license.