I want my own package function!

Hey guys. I know that this is not in the best practices manual, but I need to place a method DIRECTLY inside a package of mine. Pretty much like flash does with the as3 new flash.utils.* package methods.

Could anyone guide me on this :)… I’ve been searching around hardly with zero results.

Tanks!

I don’t remember, but:

package s.utils {
   public function doSomething():int {
      return 1;
   }
}

Ok, but when calling the method from outside, how would it be?

Maybe something like: (please confirm)

[AS]import s.utils.*
this.doSomthing();[/AS]

(Like I said on GTAL)


package com {
   
   public class myClass {
   
      public function myClass():void {
      }
      
      public static function myFunction():void {
         trace("tjohoo");
      };
   
   };
   
};

import com.myClass.
myClass.myFunction();




import s.utils.doSomething;
trace(doSomething());

http://www.mail-archive.com/flexcoders@yahoogroups.com/msg48837.html

The link you’ve reported brings that there can be only one public function per AS file. So, taking the example of flash.utils.* methods, to do something like that (with may methods directly inside a package), should I have to create multiple AS with the same package name? One for each method?

Give the .as file the same name as the function. doSomething.as, etc.

What? You can have as many public functions as you please, can’t you? I know I certainly have custom packages with a bunch of public functions in a single file. You can only have a single constructor, but that’s different.

[QUOTE=Anogar;2350429]What? You can have as many public functions as you please, can’t you? I know I certainly have custom packages with a bunch of public functions in a single file. You can only have a single constructor, but that’s different.[/QUOTE]
Are you sure that you’re talking about package functions? Obviously you can have multiple public methods in a class, but package functions don’t have an enclosing class (thus the name).

If I try to put multiple, publicly-visible functions in a file, like this:

package funcs {
	public function a(){
		trace('a');
	}
	
	public function b(){
		trace('b');
	}
}

… then I get the error “5006: An ActionScript file can not have more than one externally visible definition: funcs.b, funcs.a”, which is expected because you always name the file after the one externally visible definition.

Ah, no, you’re right. I thought he meant a class within a package. What is the benefit of just putting it directly into the package?

It’s for functions that aren’t specific to a class. Stuff like flash.utils.getTimer and trace are packaged functions because they are used throughout. Many people create singleton classes which is the same sort of idea.

Thanks everyone, It was very elucidative.