Monchoman45
Monchoman45

Reputation: 547

Namespace error: "Top level of a class definition"?

I'm trying to encapsulate a package so that classes, properties, and methods that have no meaning outside of the project aren't accessible. They do, however, have to be accessible by other classes in the project, so internal is insufficient - the internal properties of parent.foo.AClass can't be accessed by parent.bar.AnotherClass. I was under the impression that namespaces could solve this problem, so I tried this:

//Class in top level package, where we make our namespaces
package parent {
    public namespace myproject;
    internal namespace myproject_internal;

    myproject class Top {
        //code
    }
}

//Some class not meant for use outside of the project
package parent.foo {
    use namespace myproject;
    use namespace myproject_internal;

    myproject_internal class AClass {
        //code
    }
}

//Some other class that can be accessed outside of the project,
//but is in a different subpackage
package parent.bar {
    use namespace myproject;
    use namespace myproject_internal;

    myproject class AnotherClass {
        //code
    }
}

This, however, throws a compiler error in AnotherClass at the class definition (myproject class AnotherClass): "a user defined namespace attribute can only be used at the top level of a class definition." What does that mean? Are namespaces meant to be used some other way?

Upvotes: 3

Views: 509

Answers (1)

weltraumpirat
weltraumpirat

Reputation: 22604

The error is thrown, because you are trying to apply the namespace to your entire class, but user defined namespaces are limited to use with variables and functions ("top level of a class definition" means "elements within a class"). If you're going to say "But that's stupid!" I can only agree with you - it would make much more sense to have namespaces available to classes and interfaces, as well. Still: Those are the rules. :(

I also believe your namespace definition isn't done correctly. If you're going to use a namespace for more than a single class, it should be declared in a separate file. Something like:

package my_package {
    public namespace myproject;
}

in /my_package/myproject.as, and

package my_package {
    internal namespace myproject_internal;
}

in /my_package/myproject_internal.as.

Note that internal in this case specifies the visibility for the namespace itself, and not for the functions and variables you are using it with - those will be visible within the custom namespace (duh!). If you're going to use an internal namespace, your implementing class has to reside within the same package - otherwise that namespace will not be accessible.

See Grant Skinner's blog post for comprehensive info about namespaces.

Last, but not least, I would strongly advise against using "parent" as a package name - apart from it being a very ambiguous name, this could easily lead to naming conflicts when used in conjunction with display objects, where parent refers to the actual parent object.

Upvotes: 4

Related Questions