HaxeDoc2

(やまだぃちぅ) #1
In this context, atargetcan be the dot-path (3.7) to


  • aclass field,

  • aclassorabstracttype, or

  • apackage.


If it is a class or abstract type, access modification extends to all fields of that type. Likewise,
if it is a package, access modification extends to all types of that package and recursively to all
fields of these types.
1 @:allow(Main)
2 class MyClass {
3 static private var foo: Int;
4 }
5
6 class Main {
7 static public function main() {
8 MyClass.foo;
9 }
10 }


Here,MyClass.foocan be accessed from themain-method becauseMyClassis annotated
with@:allow(Main). This would also work with@:allow(Main.main)and both versions
could alternatively be annotated to the fieldfooinstead of the classMyClass:
1 class MyClass {
2 @:allow(Main.main)
3 static private var foo: Int;
4 }
5
6 class Main {
7 static public function main() {
8 MyClass.foo;
9 }
10 }


If a type cannot be modified to allow this kind of access, the accessing method may force
access:
1 class MyClass {
2 static private var foo: Int;
3 }
4
5 class Main {
6 @:access(MyClass.foo)
7 static public function main() {
8 MyClass.foo;
9 }
10 }


The@:access(MyClass.foo)annotation effectively subverts the visibility of thefoofield
within themain-method.
Free download pdf