Using other types within packages

I was doing something, I forget what it was now, but it got me curious about functions defined in some of the flash player native packages. The flash.net, flash.profiler, flash.system, and flash.utils packages all have functions defined in the package itself rather than in a class as static methods. This got me wondering if it was possible to do this with my own packages. Typically what’s defined in a package file is a class or interface which I thought were the only allowed but apparently not.

You can define a package function like so:

// file: src/nochump/util/string/repeatString.as
package nochump.util.string {

	public function repeatString(s:String, times:int):String {
		return new Array(times).join(s);
	}

}

You can then use it like this:

// file: src/MainApp.as
package {

	import flash.display.Sprite;
	import nochump.util.string.repeatString;

	public class MainApp extends Sprite {

		public function MainApp() {
			trace(repeatString("Wokka", 3));
		}

	}

}

Also, any type can be defined:

// file: src/nochump/util/math/TWO_PI.as
package nochump.util.math {

	public const TWO_PI:Number = Math.PI * 2;

}

Use this like so.

// file: src/MainApp.as
package {

	import flash.display.Sprite;
	import nochump.util.math.TWO_PI;

	public class MainApp extends Sprite {

		public function MainApp() {
			trace(TWO_PI);
		}

	}

}

Of course this isn’t really advantageous if you have to define a lot of types as you’ll need a separate file for each definition. A file can only have one externally visible definition otherwise you get an error.

I don’t know often I would use this, it’s just better to give your definitions a more specific context. Doing things this way can get back into a procedural programming style. If you do want to define things this way, be sure it’s something very generic that just deals with primitive types or types in the same package.

It’s not all bad tho, there are some small advantages. For one, it saves an extra object reference to access the definition. As in the example above, you can just call repeatString instead of something like StringUtil.repeatString. Yes, very minor. Another plus is it can save a bit of space in terms of what gets compiled into your swf. Let’s take a StringUtil class that has say 20 methods for various things like trimming, padding, and whatever else but you only need to use one of those methods. Rather than importing the whole class you can try something like the approach presented here. Of course, you can always just copy and paste that method to your project somewhere.

Anyways, I wasn’t sure if others have covered this. Heck, it’s probably right there in in the manual or ECMAScript specification but whatever. Just sharing what I learned today. :)

No Trackbacks

One Comment

  1. Thanks for reminding me how this works.

    When AS3 first came out, I was a little bit annoyed that trace() was a package function thinking methods belong to classes.

    Now months later, I find myself the need to make a package function of my own.

    Posted April 25, 2007 at 10:46 pm | Permalink