Remix.run Logo
▲ binary132 a day ago

I was surprised reading these comments to see hardly any mention of type invariance, and no mention at all of RAII. RAII gets you type invariants that hold for the entire lifespan of an object, and if anything goes wrong during initialization, you are guaranteed to release all partial state according to its specified rules — giving you something closer to whole-program invariants. We all love POD / structs, but they don’t get you that part, and doing it without RAII can land anywhere from a bit of meticulous bookkeeping all the way to building an intractable metaprogramming malebolge just to get half-baked DIY RAII.

▲aw1621107 a day ago | parent [-]

You don't really need C++-style constructors for RAII though. Rust implements similar semantics as C++ in that regard and constructors aren't a thing there.

▲layer8 a day ago | parent | next [-]

Rust doesn’t have implementation inheritance. If it did, it would need some constructor-like concept as well to enforce encapsulation between inherited implementation and inheriting implementation.

▲aw1621107 a day ago | parent | next [-]

Even if you assume that's true that seems more like "constructors are needed for implementation inheritance" than "constructors are needed for RAII".

I'm not even sure about the former? The important bit is that base class invariants can't be observed in an invalid/inconsistent state, and regular visibility/privacy/etc. rules seem technically sufficient for that? I'm not sure where exactly the boundaries of "constructor-like concept" are supposed to lie.

▲ninkendo a day ago | parent | prev [-]

Objective-C has implementation inheritance but doesn't have constructors, I don't think constructors are essential there. As I said in the OP, `Cow::new()` can easily just call `Animal::new()` before continuing to initialize its own fields, etc.

Heck, Smalltalk is the original OOP language and has implementation-based inheritance, and construction is done through ordinary methods on class objects.

▲layer8 a day ago | parent [-]

Objective-C plays it fast and loose here. It doesn’t enforce object initialization, hence client code including subclasses can create and use uninitialized instances.

Smalltalk is even worse, but it also has the philosophy that anything can be patched at runtime, so it kind of is in line with that.

This is basically about static type safety.

You could in principle enforce proper initialization and encapsulation by static analysis requirements instead of by a dedicated language construct. The latter is traditionally more efficient however, and arguably makes the code more explicit about its intent.

▲binary132 20 hours ago | parent | prev [-]

I mean, by that logic, so does Lua, but nobody goes around saying Lua has RAII.

▲aw1621107 9 hours ago | parent [-]

I'm not super familiar with Lua so just to make sure I'm understanding you correctly are you referring to to-be-closed variables?

If so, I think the difference is that in C++/Rust RAII is the default and is quite pervasive as a result. To-be-closed variables seem like IDisposable in C# or try-with-resources in Java in that they allow the programmer to use RAII-like semantics but are opt-in.

▲binary132 6 hours ago | parent [-]

yes, just like in Rust, where Drop and constructors are optional idioms.