49 ms·
Ok but it's 2022 and something like ziglang can do all of these things with two keywords (const and comptime). Why does C++ need four?
by throwawaymaths 4y ago
Ok but it's 2022 and something like ziglang can do all of these things with two keywords (const and comptime). Why does C++ need four?
- jeffbee 4y agoBecause of path dependency and backwards compatibility. C++ has 30 years of legacy code to build. Zig doesn't even exist yet; after 6 years it is still in "preview".
- kllrnohj 4y agoIt 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.