Weird static function main() behavior - why do singletons get instantiated first?

I’m using hexLog and initializing it in static function main()

I also have some singletons in my application. The annoying thing that happens is that the singletons are initialized before even the very first line in static function main(), which is quite weird to me.

It also causes my hexLog to miss a number of logs. For some other weird reason I don’t get, it starts logging after a while.

I can debug the hexLog myself I think, but not until I understand this behavior.

I checked the manual for the singleton pattern and for static but don’t get anything meaningful out of that.

BTW my target is Flash for now (doing some early prototyping and didn’t bother to set up VS Code yet).

I don’t know anything about hexLog, but in general, in Haxe, static variables are always initialized before the main function call (i’m not 100% sure on this for targets with native static ctors like C#, but at least on JS it’s always like that), so if the singletons you’re talking about are created as static fields - then they will surely be created before calling main.

I think there are two things to try here:

  • make your singleton initialization explicitly depend on the loggers, then Haxe will sort and init statics in the correct way
  • use the magic __init__ static function to do early initialization. that will be executed even before static fields, so it might do the trick. you have to be careful though, because, well, nothing is really initialized at the moment it’s called