In C/C++ I can do something like this:
class Foo {
#ifndef NDEBUG
bool initialized;
#endif
void init() {
// do stuff
#ifndef NDEBUG
initialized = true;
#endif
}
void deinit() {
// do stuff
#ifndef NDEBUG
initialized = false;
#endif
}
void doSomething() {
#ifndef NDEBUG
assert(initialized == true && "You forgot to call init()!");
#endif
}
};
How do I do this in Zig? The only thing I can come up with is this:
fn Foo() type {
if (builtin.mode == builtin.Mode.Debug) {
return struct {
initialized: bool = false,
// everything else goes here
};
}
else {
return struct {
// duplicate everything AGAIN, except without checks for "initialized"
};
}
}
but this means I have to have 2 copies of my struct, and 2 different sets of code to maintain.