Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3 internal and custom namespaces

I have the following packages:

spark
    spark.engine

Within spark I have a class SeCore; and within spark.engine I have SeStepper and SeKeyboard.

What I'm trying to achieve is have SeCore as being the only class that can create an instance of SeStepper or SeKeyboard. This can be achieved by moving SeCore into the spark.engine package and making the other two classes internal, but I'd like to have SeCore in the spark package if possible.

I've tried making my own namespace to handle this, like so:

package spark.engine
{
    import spark.namespaces.spark_core;

    use namespace spark_core;

    spark_core class SeStepper extends SeObject
    {
        //
    }
}

However I get the error:

1116: A user-defined namespace attribute can only be used at the top level of a class definition.

Are there any other approaches I can take to achieve what I'm after?

like image 727
Marty Avatar asked Feb 15 '26 17:02

Marty


1 Answers

99% of the time, marking anything as 'internal' is a bad idea. It's better to have a naming convention for 'off-limits' classes and members, and allow developers to go there at their own risk. Marking things as 'internal' or 'private' is something that should only be done rarely, and with great forethought.

However, you could enforce this behavior at run time by using a read-only property in SeCore and checking its value from SeStepper and SeKeyboard.

Following is pseudocode, haven't used AS3 in a while.

In SeCore

private var _createAuthorized = false;
public function get CreateAuthorized():boolean {return _createAuthorized;}

private function createSeStepper(){
  _createAuthorized = true;
  var obj = new SeStepper(this)
  _createAuthorized = false;
  return obj;
}

in SeStepper

public function SeStepper(core:SeCore){
  if (!core.CreateAuthorized) throw new Error("Only SeCore can do this");
}
like image 146
Lilith River Avatar answered Feb 19 '26 09:02

Lilith River



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!