Introspection and private namespaces in AS3

Hello!

Today, we are going to discuss introspection and private namespaces in AS3/Flex!

 

The motivation to use private namespaces in the context of introspection is to be able to expose API implemented in your class in a way that is not public (everyone can use it), protected (only inheriting classes can use it) or private (only the class can use it).

 

Consider this: You are implementing a framework, and one of the classes (ClassA) is used both by the framework users (external) and within the framework (internal). That means that all the functionality in ClassA that is intended for external use must be public. Unfortunately, so does the functionality intended for internal use.

 

If you would like to prevent exposing the internal API to the outside world, but still use it internally, private namespaces is the way to go!

 

Basically, these are the steps to use private namespaces:


1. Define the namespace:

namespace nameSpace;

 

2. Use the namespace instead of an access control specifier in a variable or function declaration:

namespace function myFunc() {}

 

3. Now, use it:

use namespace namespace;

myFunc();

 

 

So, without further ado, let's get down to business.

 

First, we'll create the namespace (file namespace/nameSpace.as):

package namespace {

   // URIs default to {package}.{namespace defined in this file}, I am overriding this here.

   public namespace nameSpace = "com.tikalk.private.namespace";

}

 

 

Second, we'll create the ClassA mentioned above (file example/ClassA.as):
 

package example {

   import namespace.nameSpace;

   public class ClassA {

    nameSpace function func1():void {}
    nameSpace var tempVar:Object;
    public function func2():void {}
   }
}

 

Now, any class ClassB that uses ClassA will be able to access func2.
But, to access func1 and tempVar, ClassB would have to import the namespace and use it:

import namespace.nameSpace;
use namespace nameSpace;
func1();

 

I have created an example, will post it later today.

 

Thank you for your interest!

We will contact you as soon as possible.

Send us a message

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com