Error trying to implement singleton with macros

Hello i’m fairly new with programming in haxe/openfl and i tried to implement singletons via macros. I found this https://github.com/player-03/Singleton and i keep getting an error that the type name singleton is already defined in this module. I tried fiddling with code but i just can’t get it working. Thanks in advance.

It’s possible that you have another “Singleton” class out there somewhere. Do you have DelishusCake’s version installed too?

One thing to try is adding a package to Singleton.hx. If you do, remember to put it in a matching folder, and to import it whenever you want to use it.

Thanks for replying.

I noticed i was using the old version from DelishusCake so i changed it to your version.(with all the trying to get it to work properly) Your version works fine when it has the default package. But when i try to put it in a folder and change the package, @:autobuild can’t find the Singleton class.

You may as well just use the working setup, but if you do want to use the package, update line 6 (probably line 7 now) to include the package name.

package com.example.package;
import haxe.macro.Context;
import haxe.macro.Expr;

#if !macro

@:autoBuild(com.example.package.Singleton.build())
interface Singleton {}

//...

Thanks.

Now that i see the solution, it seems kind of obvious.
Don’t really know much about macro’s.
I will certainly look further into it. :wink: