The Haxe language has a rather confusing article which states that the behaviour of private field access is more or less a protected
field accessor, which makes me wonder what the point is of using a private field accessor.
Protected fields are a very rare case scenario, and only ever often used if you want to keep member visibility restricted to internal access via derived classes. For those of you who do not know, a derived class is a class that is extended by another. Taking the example from the article above:
class Base {
public function new() { }
private function baseField() { }
}
class Child1 extends Base {
private function child1Field() { }
}
class Child2 extends Base {
public function child2Field() {
var child1 = new Child1();
child1.baseField();
// Cannot access private field child1Field
child1.child1Field();
}
}
The Base
class is a derived class from Child1
, because Child1
extends it. So by derived class, the variable in Child2
's public function called child1
is derived from Base, but you cannot access the private function of Child1
because you can only access protected variables from a derived class.
This is confusing, because there is no mention of the keyword protected
even though the private
field accessor is considered as such.
What’s more confusing is that if you want to keep certain functions internal because it makes no sense for it to be accessed outside of the scope of the application or library, that the only way to do so would be to make a dummy base class which is then extended by what is otherwise the derived class in any instance.
If you were doing auto-completion in FlashDevelop, the “@:noCompletion” meta-data is a silly workaround to prevent private access fields from being displayed in completion, but does not actually prevent still being able to use that field outside of the intended scope.
Personally, I think Haxe needs to properly implement the protected
keyword and have private
actually do what it says on the tin, which is to make visibility private, not protected. Is it me, or do the Haxe developer’s not know how visibility works?