views:

36

answers:

1

In AS3, why do we put our import statements in the package statement, and not inside the Class declaration?

This is a typical (but rather pointless) AS3 class:

package my.foo.package
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.TimerEvent;
    import flash.utils.Timer;

    public class FooClass extends MovieClip
    {
        public static const EVENT_TIME_UP:String = 'timeUpEvent';

        public function FooClass() 
        {
            var timer:Timer = new Timer(2000, 1);
            timer.addEventListener(TimerEvent.TIMER, timerHandler);
            timer.start();
        }

        private function timerHandler(e:TimerEvent):void 
        {
            dispatchEvent(new Event(FooClass.EVENT_TIME_UP));
        }
    }
}

But why are all the import statements meant to go all the way up there, outside on the Class? The class works perfectly fine when I move the imports to inside the class declaration like below.

package my.foo.package
{
    import flash.display.MovieClip;

    public class FooClass extends MovieClip
    {
        import flash.events.Event;
        import flash.events.TimerEvent;
        import flash.utils.Timer;

        public static const EVENT_TIME_UP:String = 'timeUpEvent';

        public function FooClass() 
        {
            var timer:Timer = new Timer(2000, 1);
            timer.addEventListener(TimerEvent.TIMER, timerHandler);
            timer.start();
        }

        private function timerHandler(e:TimerEvent):void 
        {
            dispatchEvent(new Event(FooClass.EVENT_TIME_UP));
        }
    }
}

It's only the MovieClip import that needs to be in package, because my class is extending it.

There is nothing helpful about this in the Adobe AS3 coding conventions.

So why do we put the imports in the package and not the class that's actually using them. The package is not using the imported classes is it? And why does it still work if I move the imports into the Class?

A: 

This is simply a convention that's in use so there is one central place to view all of the imports. Let's say we take your logic to the next extreme and place an import statement on the line just before every variable we declare (which compiles just fine by the way). We'd wind up needlessly duplicating import statements whether on purpose or by accident. Also, someone wanting to see what dependencies a given file has would have to scan the whole file rather than looking at just the top of the file. Your point of putting the imports within the class declaration isn't as crazy, but you'd still wind up with the problem of imports scattered throughout a file if your file declares more than one class (as in the case of internally used classes, for example).

Wade Mueller
Agree with the post except the last parenthesized part - you should import classes again for the internally used classes (private classes outside package statement block). import statements inside package block aren't visible outside the package block.
Amarghosh