7 ms·
This article compares the hierarchical nature of the type system of OOP languages (think Java) with the hierarchical nature of ownership in languages with Owner
by correct_horse 6y ago
This article compares the hierarchical nature of the type system of OOP languages (think Java) with the hierarchical nature of ownership in languages with Ownership/Move semantics (Rust and newly C++). The author notes that "composition over inheritance" is a rejection of the strictly tree-shaped OOP type system. In the same way, Rust's `RefCell` is an escape hatch from the strict hierarchy of ownership, maintaining a run-time invariant of one owner rather than a compile-time invariant.
One difference I see between these two escape hatches are switching costs. Rewriting the Java standard library to use composition and not inheritance borders on impossibly difficult. Rewriting the rust standard library to use RefCell instead of &mut everywhere would be arduous and possibly automatable, but not impossible.
- ncmncm 6y ago"...and newly C++"? C++11 had move semantics. So did C++14 and C++17. C++20 still does. It is a stretch to suggest Rust had them longer, considering Rust 1.0 came out well after C++11, and cribbed them. It is a shame that C++ can't be move-by-default and const-by-default, but that's backward compatibility for you. Rewriting the Rust std lib to use RefCell everywhere would be possible, but would make it almost entirely useless.
- gpderetta 6y agoMove semantics even predate C++11. The need for move only types was identified very and such types have existed in various form for a ling time.
- bregma 6y agoC++98 had move semantics using std::auto_ptr.
- correct_horse 6y agoI probably shouldn't have said newly C++, but my frame of mind was relative to the lifespan of the language. > Rewriting the Rust std lib to use RefCell everywhere would be possible, but would make it almost entirely useless. Maybe not useless, but certainly worse than it currently is. The point is that you can switch one function to using RefCell (a breaking change, but) totally possible
- ncmncm 6y agoI would admit "and lately, C++".