I like to be able to do more compile-time evaluation and it's definitely a lot less confusing now than abusing templates for that.
I like to be able to do more compile-time evaluation and it's definitely a lot less confusing now than abusing templates for that.
The comments all seem like just mindless anti-C++ circle jerking, with zero relevance to what's actually being discussed in the article. But that's sadly quite common with C++ articles here. If the title has C++, expect to see the same rants regardless of the article.
As for `constinit`, it looks like ziglang doesn't have an answer for the problem that constinit solves. That is, ziglang appears to have the same bugs here that C++ did before constinit was added.
zig has lazily initialized at runtime mutable globals, and compile-time initialized constant globals (so C++'s constexpr variables). But what it doesn't have is compile-time initialized mutable globals (which is what `constinit` does)
// compile-time initialized mutable global:
var foo = bar();I'm not sure how to reconcile that Zig documentation with your comment's claim. It appears that Zig relies on opportunistic behaviors here instead of contracts. That is, your var foo could be comptime known, but isn't required to be. Which is fine enough, but not comparable to `constinit` either.
Alternatively if all container variables are exclusively comptime initialized, then the obvious missing part from Zig would be the inverse of constinit which is the onload initializer behavior.
When is onload? I don't think that's a time that occurs during program execution in Zig.
If not then that'd be the 3rd type of initialization it's missing, the equivalent of C++'s nothing specified.
You can order the init sections on your own when you generate the binaries and add your own constraints, and if your toolchain doesn't give you that control, you can generate and call your own init functions. (Since I didn't want to dick with the linker too much, I generated my own init functions in my natively compiled language to solve this problem).
Your language's dlopen wrapper can also dlsym the initializer and call it if it's present.
This is very much a C++ problem, because its separate compilation model throws away initializer ordering, and prevents the compiler from doing anything about it. And, of course, compatibility makes this a tough sell for C++ -- languages unconstrained by history don't have to worry about this.
tl;dr: Ignore the constructor attribute/init section, and generate your own function that orders things correctly. Then call it before main.
How does my shared library do that?
def loadlib() {
lib = dlopen("libfoo.so")
sym = dlsym(lib, "pkgname.__init__")
sym()
return lib
}
Hell, you can even do this with a single constructor attribute that runs the initializer, if you want to be fully dlsym compatible.If you have a DAG of dependencies, it's impossible for your library to care whether main or dlopen has called its initializer, because the same initialization order of dependencies is observed in either case. The only thing you need to beware of is guarding against double-init if you dlopen, and the generated library-level initializer code can do that.
If you generate a single initialization symbol that guards against multiple runs in the init section, this works just fine with dlopen.
This isn't even hard, but you need to be able to form a full dag of imports.
The only problem is that the linker slams together the .init, .ctors, or .init_array sections in some arbitrary order (usually the order that .o files are passed in on the command line, but there are no guarantees). If you topologically sorted the object and library list by import order, things would work out. But because the linker doesn't cooperate, a language that wants to avoid the initialization order fiasco needs to provide its own ordering. Or its own linker.
As far as Zig goes -- I have no clue what it does. Never touched it.
Also, as a side note -- initializers were initially designed so that there would be exactly one init function per binary or library, and then people wanted to split the initialization up, so the way the init section ends up getting constructed on most unixes is fascinatingly hacky: The linker links `crti.o`, your .o files, `crtn.o`. crti.o contains a function prologue; the init sections in your .o files contain a sequence of call functions, and crtn.o contains a function epilogue. Stitch them all together in order, and you get a single function that you can call.
This is deprecated, and now there's a table of function pointers (.init_array).
This is a very widely used capability and it is very definitely not trivial to just make an init function & define the import dag and have that work reliably. People will forget to call it & most of the time they won't have any clue what their dependency DAG even is. And that's assuming it can even form a DAG at all - cyclic dependencies are absolutely a thing, after all.
And I'm not sure how you can forget to import a library and still expect to use it, assuming your language has a module system.
C and C++ can't solve this problem, of course, due to textual inclusion.
no, it isn't because your library has to integrate with other languages when you make native stuff. I should be able to call dlopen from any native language without having to care in which language your library was implemented, just calling my OS's dlopen / LoadLibrary OS API function should be enough. If this doesn't work, then your solution is just not acceptable in many cases, so yes you have to work with its limits.
People want to initialize stuff at load library time. They will always want to do this. If the language doesn't provide a solution, people will just bolt on shitty add-on toolchains to do it anyway.
This isn't a uniquely C++ problem (__attribute__((constructor)) is after all for C, not C++).
Take for example Rust, which also doesn't provide an attribute((constructor)) equivalent and says nothing happens before or after main. Except https://crates.io/crates/ctor exists to immediately say "fuck that noise" and racked up >20m downloads.
You realize Java has the feature you're discussing, without dependency injection?
class Foo {
static {
initialize();
}
}
They even have a well defined execution order:https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.htm..., section 8.7, with order defined in section 12.4
So no, that's not equivalent. Not even close. This is why you see things in Java that scan through loaded jars looking for all classes with attributes to just speculatively load. To recreate what c++ has natively & C has with a broadly supported extension.
Consider: C++ let's an increasingly large subset of the language be evaluated at compile.
Current endpoint: the whole language will have constexpr everywhere.
Correct design: Eagerly try to evaluate everything, ban things that obviously shouldn't or can't be done at compile time.