views:

34

answers:

3

After I saw the methods in flash.utils package, I decided to make a try:

// inside file Test.as

package com.tests
{
 internal function packageFunction() {

 }
}

But I got:

A file found in a source-path 'Test' must have the same name as the class definition inside the file 'packageFunction'.

packageFunction is a function, not a class.. Any idea what is wrong?

--update

For those who don't know what is the "package function" I'm talking about, please, see the flash.util package methods. I can use it like:

flash.utils.getTimer();
A: 

You are missing your class definition:

package com.tests {
    public class Test {
        internal function packageFunction() {

        }
    }
}
sberry2A
this is a class function, not a package function.. I can't access it using com.tests.packageFuncion().. please, see the [flash.utils package](http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/utils/package.html) to understand what I'm talking about.. ;)
Tom Brito
+1  A: 

Your code is in Test.as. It should be in

com/tests/packageFunction.as

This follows the same rules as "normal" classes. The package and main public symbol name must mach the directory path and the file name.

Juan Pablo Califano
so, why the [flash.utils package](http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/utils/package.html) doesn't need to have each method inside a file with the same name? For you who upvoted too.. ;)
Tom Brito
also, even if the first method have the same name as the package, the second give an error telling that "A file found in a source-path can not have more than one externally visible definition."
Tom Brito
@Tom Brito. flash.utils is a package. It can contain public symbols such as classes, interfaces, functions, variables and constants. Each of these symbols must be defined in its own file. There's no actionscript code for getTimer() or other code native to the player. I'm not sure where did you get the idea that "flash.utils package doesn't need to have each method inside a file with the same name"...
Juan Pablo Califano
@Tom Brito. Added to my previous comment. There can be only *one* externally visible symbol in each as file. So you can't have two visible functions in the same file (you can have other functions, classes, etc in the same file, but outside the package definition. Also, a little correction in your terminology: these are not methods but functions.
Juan Pablo Califano
A: 

If your code is in the Test.as class , you already are inside a package and you can't have another package inside or outside a package, what you can do is have another Class outside the package

   package com.tests
   {
       public class Test
       {
           private var example:ClassExample = new ClassExample();
       }
   }

   //This class is only accessible in the Test class
   class ClassExample
   {
         public function ClassExample()
         {
         }
   }

The example you have given is a misunderstanding of the use of packages. The functions described in the docs are accessible within any package , it doesn't mean you can create a package in an existing class to access these functions.

PatrickS