Search code examples
actionscript-3game-enginehaxe

Restricted access of function in Haxe?


I started playing around with Haxe recently, after using AS3 for quite some time and ran into a problem while writing a very simple game engine:

I have a class called World. One of the things the World does, is keeping track of all Entity objects in the game. I want this list of Entity objects to be accessible ONLY from within the engine. The user of the engine should not be able to modify the list directly.

In AS3, I could simply use the internal keyword to give access to other classes within the same package. And when that wasn't enough I could define a custom namespace and use it as my access modifier. But Haxe doesn't seem to have either of those.

TL;DR: How can I restrict access of a variable to a specific package/namespace? If not possible, what other options do I have?


Solution

  • If you're using Haxe 2.11 (nightly build), you can use @:allow.

    Copied from the Haxe.org wiki:

    @:allow(my.pack) : This will give access to all private fields of a class to all the classes in the package my.pack (and its sub-packages). See Access Control for more info. (since 2.11)

    More detailed doc on Access Control.