Skip to content

Repository files navigation

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:

classMySingletonClass
{
...methodshere...publicstaticvar__instance:MySingletonClass;
publicstaticfunctionget_instance():MySingletonClass
{
if (MySingletonClass.__instance==null)
MySingletonClass.__instance=newMySingletonClass();
returnMySingletonClass.__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:

classMySingletonClassimplementsSingleton
{
...methodshere...
}

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();

About

Haxe macros for creating singleton classes.

Resources

Stars

8 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages