# I want my own package function!

**URL:** <https://forum.kirupa.com/t/i-want-my-own-package-function/264620>\
**Category:** flash\
**Created:** [June 27, 2008, 8:13pm UTC](https://forum.kirupa.com/t/i-want-my-own-package-function/264620 "2008-06-27T20:13:13Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![ad\_sign](https://avatars.discourse-cdn.com/v4/letter/a/ac91a4/32.png) [@ad\_sign](https://forum.kirupa.com/u/ad_sign)\
**Post date:** [June 27, 2008, 8:13pm UTC](https://forum.kirupa.com/t/i-want-my-own-package-function/264620/1 "2008-06-27T20:13:13Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Felixz](https://avatars.discourse-cdn.com/v4/letter/f/ecb155/32.png) [@Felixz](https://forum.kirupa.com/u/Felixz)\
**Post date:** [June 27, 2008, 8:46pm UTC](https://forum.kirupa.com/t/i-want-my-own-package-function/264620/2 "2008-06-27T20:46:57Z")

</div>

I don’t remember, but:

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

```

---

<div class="post-metadata">

**Author:** ![ad\_sign](https://avatars.discourse-cdn.com/v4/letter/a/ac91a4/32.png) [@ad\_sign](https://forum.kirupa.com/u/ad_sign)\
**Post date:** [June 27, 2008, 8:56pm UTC](https://forum.kirupa.com/t/i-want-my-own-package-function/264620/3 "2008-06-27T20:56:09Z")

</div>

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

Maybe something like: (please confirm)

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

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [June 27, 2008, 9:06pm UTC](https://forum.kirupa.com/t/i-want-my-own-package-function/264620/4 "2008-06-27T21:06:38Z")

</div>

(Like I said on GTAL)

```auto

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

```

import com.myClass.  
myClass.myFunction();

```auto

```

---

<div class="post-metadata">

**Author:** ![Felixz](https://avatars.discourse-cdn.com/v4/letter/f/ecb155/32.png) [@Felixz](https://forum.kirupa.com/u/Felixz)\
**Post date:** [June 27, 2008, 9:06pm UTC](https://forum.kirupa.com/t/i-want-my-own-package-function/264620/5 "2008-06-27T21:06:58Z")

</div>

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

```

[http://www.mail-archive.com/flexcoders@yahoogroups.com/msg48837.html](http://www.mail-archive.com/flexcoders@yahoogroups.com/msg48837.html)

---

<div class="post-metadata">

**Author:** ![ad\_sign](https://avatars.discourse-cdn.com/v4/letter/a/ac91a4/32.png) [@ad\_sign](https://forum.kirupa.com/u/ad_sign)\
**Post date:** [June 27, 2008, 9:33pm UTC](https://forum.kirupa.com/t/i-want-my-own-package-function/264620/6 "2008-06-27T21:33:58Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [June 27, 2008, 9:58pm UTC](https://forum.kirupa.com/t/i-want-my-own-package-function/264620/7 "2008-06-27T21:58:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)\
**Post date:** [June 27, 2008, 10:13pm UTC](https://forum.kirupa.com/t/i-want-my-own-package-function/264620/8 "2008-06-27T22:13:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [June 27, 2008, 10:28pm UTC](https://forum.kirupa.com/t/i-want-my-own-package-function/264620/9 "2008-06-27T22:28:28Z")

</div>

[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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)\
**Post date:** [June 27, 2008, 10:44pm UTC](https://forum.kirupa.com/t/i-want-my-own-package-function/264620/10 "2008-06-27T22:44:12Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [June 28, 2008, 4:09am UTC](https://forum.kirupa.com/t/i-want-my-own-package-function/264620/11 "2008-06-28T04:09:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ad\_sign](https://avatars.discourse-cdn.com/v4/letter/a/ac91a4/32.png) [@ad\_sign](https://forum.kirupa.com/u/ad_sign)\
**Post date:** [June 28, 2008, 12:53pm UTC](https://forum.kirupa.com/t/i-want-my-own-package-function/264620/12 "2008-06-28T12:53:12Z")

</div>

Thanks everyone, It was very elucidative.
