Haxe macros for creating singleton classes.
Haxe
Switch branches/tags
Nothing to show
Latest commit 9e4719f May 21, 2012 @aszlig aszlig Add haxelib related stuff.
Permalink
Failed to load latest commit information.
singleton/macros
.gitignore
COPYING
README.md
Singleton.hx
Test.hx
build.sh
haxelib.xml
test.hxml

README.md

Easy singleton instances for Haxe

Usually singleton classes involve quite a bit of boilerplate code to retrieve and implicitly create the corresponding instance, like for example:

class MySingletonClass
{
    ... methods here ...

    public static var __instance:MySingletonClass;

    public static function get_instance():MySingletonClass
    {
        if (MySingletonClass.__instance == null)
            MySingletonClass.__instance = new MySingletonClass();
        return MySingletonClass.__instance;
    }
}

So all calls to the singleton instance would look like that:

MySingletonClass.get_instance().some_method();

Of course this could also be done using properties, but it just avoids a few characters (two parenthesis) when retrieving the instance.

Our approach

Thanks to macros we can avoid those boilerplates and repetitious code. Using this library you define a singleton class like this:

class MySingletonClass implements Singleton
{
    ... methods here ...
}

This turns all public fields of the class into static functions which automatically work on the right instance. So calls to the singleton instance look like that:

MySingletonClass.some_method();