Remix.run Logo
▲ renox 2 hours ago

So Zig makes 'build-in-place' hard?

That's difficult to believe for a C-like language..

▲wavemode an hour ago | parent [-]

It's a tradeoff thing. In languages like C and Zig you can declare a struct (which, naturally, will start out containing whatever random garbage was at that memory location) and then just begin assigning values to its fields.

But in C++ and Rust, assignment itself is a method which can be overridden. So what does it mean to call a method on an object that doesn't exist yet? Also, destructors have to be run when an object goes out of scope. But what if an error occurs before all the fields have been assigned - now you're destructing objects that don't semantically exist yet, and whose memory contains garbage.

That's why I say it's a trade-off - if they wanted, the designers of C++ and Rust certainly could have just allowed programmers to paint themselves into such corners. But they preferred to disallow these things entirely and instead require objects to be constructed all at once as a semantic unit.

▲kibwen 33 minutes ago | parent [-]

To clarify, Rust doesn't allow overloading the assignment operator. The compound arithmetic assignment operators like `+=` are overridable, but that doesn't pose any problem for the use-before-init analysis, which already prevents all methods from being called on uninitialized variables.