10 ms·
// compile-time initialized mutable global: var foo = bar();
by AndyKelley 4y ago
// compile-time initialized mutable global:
var foo = bar();
- lvass 4y agoHow does one assert bar() or better yet, an arbitrary expression you place there, is pure?
- anonymoushn 4y agoWell, the compiler attempts to evaluate bar(), and if it cannot, you get a compile error
- kllrnohj 4y ago> If a container level variable is const then its value is comptime-known, otherwise it is runtime-known. 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.
- anonymoushn 4y agoThe quoted documentation says if you initialize a mutable variable to 5 at compile time and later change it to 2 and print it, it won't print 5, because uses of that variable in expressions won't be resolved to the constant value used to initialize it at compile time. When is onload? I don't think that's a time that occurs during program execution in Zig.
- kllrnohj 4y agoDoes Zig not have an equivalent of `__attribute__((constructor))`? If not then that'd be the 3rd type of initialization it's missing, the equivalent of C++'s nothing specified.
- ori_b 4y agoIt's relatively easy to solve the problem in a language with acyclic imports by doing a topological sort of the imports, and initializing in that order. In C++, with textual includes and no standardized control over the linker, you don't have the information or well-defined control to order the initialization, so you have a problem.
- jcelerier 4y agoOkay but we're talking about native languages where you are bound by the rules of your platform's dlopen / LoadLibrary implementation ; this is where so many static initialisation bugs lurk, when you load libraries dynamically
- ori_b 4y agoNo, you're not. The fact that a misfeature exists in the toolchain doesn't force you to use it. 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.
- kllrnohj 4y ago> 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?