#replacing container-level `var`
1 messages · Page 1 of 1 (latest)
If what you want is a singleton, that's the default approach. If you want a non OOP approach, then it depends on what you want to do
I do wan singletons, but zls marks them (container-level vars) and I was curius if there was a "better" approach to singletons
Not really, the better approach is not using singletons
There is also std.once for only calling a function once
is there any good reason to heap allocate the instance?
It's OK to have just one of something and not make it a singleton.
in my codebase i heap allocate so that i can initialize the members later
this might be a better example
but if you just use var instance: SomeStruct, you are still able to change its member later i think
you're right, i dont remember if there was any other reasons why i heap allocate - i just did it at the beginning and stuck with it
i just switched all those instances to not be pointers and everything works fine
you can initialize a SomeStruct in main and pass it down the call stack to wherever it is needed
yeah but there are a lot of them
all of these have some "instance" inside of them (except driver.zig, which defines the interface all the rest use)
make a GameState struct that holds them all and pass pointers to that around :P
thats generally what i do
I do that as well, lol (although I separate by scenes)
thats actually a really good idea