3 ms·
It looks like in zig all functions are `constexpr` ? As for `constinit`, it looks like ziglang doesn't have an answer for the problem that constinit solves. Th
by kllrnohj 4y ago
It looks like in zig all functions are `constexpr` ?
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.
- anonymoushn 4y agoCan you give an example of a bug in a Zig program caused by the lack of constinit? It seems like Zig just does not have the delayed initialization of static and thread-local variables that C++ has, but I'm not completely sure of this.
- kllrnohj 4y agoUnless I'm misreading this: https://ziglang.org/documentation/master/#Container-Level-Variables https://ziglang.org/documentation/master/#Container-Level-Va... 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)
- 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