I have been running into a problem with AS2 classes that depend on each
other, like a container where the sub-items know about the container. As
soon as I try to declare variables in either class so that the two
classes have a compile-time cyclic dependency, Flash chokes and produces
a meaningless error message.

I'm using MX2004 Pro - version 7.2. I can't be the first person to come
across this, so hopefully someone has a workaround. Please let me know
if there is one, because it's driving me insane!

Here's a simple example to illustrate the problem. The following code
_will_ compile, but only because of an ugly hack:

//-- FruitBasket.as
    class FruitBasket {

        var _contents;

        public function FruitBasket() {
            _contents = [];
        }

        public function addFruit( aFruit : Object ) : Void {
            _contents[ _contents.length ] = aFruit;
            aFruit["putInBasket"]( this );
            trace( "There are now " + _contents.length + " fruit(s) in
the basket." );
        }

        public function rust() : Void {
            trace( "The fruit basket is rusty!" );
        }
    }

//-- Fruit.as
    class Fruit {
        private var _container : FruitBasket;

        public function Fruit() {
            _container = null;
        }

        public function putInBasket( aBasket : FruitBasket ) : Void {
            _container = aBasket;
        }

        public function rot() : Void {
            trace( "The fruit is rotten!" );
            if( _container ) _container.rust();
        }
    }

//-- Demo code:
    myBasket = new FruitBasket();
    myFruit = new Fruit();

    myBasket.addFruit( myFruit );
    // Output (from FruitBasket): "There are now 1 fruit(s) in the basket."

    myFruit.rot();
    // Output (from Fruit): "The fruit is rotten!"
    // Output (from FruitBasket): "The fruit basket is rusty!"

That's all fine and good, but it only works because I hacked the code so
that one of the classes doesn't contain any references to the other one.
The most important line involved in the dirty hack is this one:

        public function addFruit( aFruit : Object ) : Void {

That should actually be:

        public function addFruit( aFruit : Fruit ) : Void {

However, when you make this single change, Flash chokes and spits out
the following error message:

    **Error** D:\Projects\ActionScript\FruitBasket.as: Line 1:
    The name of this class, 'FruitBasket', conflicts with the
    name of another class that was loaded, 'FruitBasket'.
         class FruitBasket {

Clearly this is meaningless, because the actual error (whatever it is)
has nothing to do with the class conflicting with itself! This seems to
be an artifact of the cyclic dependency and Flash presumably confusing
itself as it tries to resolve the symbols for both files at once.

This is a big problem for me, because I have a lot of interdependent
classes and I don't want to forgo strong typing because of a compiler
bug (although I have had to for now, with some of them). Any suggestions?

- Robert
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to