5 ms·
Don't C++ and Rust offer the same facilities? C++ has std::weak_ptr and Rust has std::rc::Weak.
by mikeash 10y ago
Don't C++ and Rust offer the same facilities? C++ has std::weak_ptr and Rust has std::rc::Weak.
- pjmlp 10y agoYes, but they are library calls without any help from the compiler.
- mikeash 10y agoHow does that make them any more difficult to use?
- fauigerzigerk 10y agoYou have to manually optimize away unnecessary reference counting without help from the compiler.
- mikeash 10y agoThat is true, but a totally different issue from breaking or avoiding retain cycles.
- pcwalton 10y agoI have never seen unnecessary reference counting be a problem in Rust. That's because not manipulating the reference count requires an explicit action—.clone()—and borrowing is so expressive that you rarely need to manipulate the counts.
- fauigerzigerk 10y agoI guess that's because in Rust you never need defensive reference counting thanks to the borrow checker.
- Rusky 10y agoRust's borrow checker actually helps with this a lot- you can hand out references to the Rc-ed data without modifying the count, and the compiler will enforce that the count is not decremented until the references are dead. Combined with the fact that plain references are more idiomatic than Rc/Arc, this leads to pretty similar results.
- pjmlp 10y agoThe compiler can provide better error messages and guide the user, whereas with library types, at least on C++'s case it requires help from external tooling. With Rust, while you can make use of the type system, it is harder for the compiler to provide such guidance, unless the types are somehow blessed.
- mikeash 10y agoWhat sort of guidance does Swift provide here? And nothing prevents Rust or C++ compilers from becoming aware of these types and providing specialized diagnostics for them.
- pjmlp 10y agoI said it can, not that it does it already. My Swift experience is constrained to the GNU/Linux version, so I don't know if XCode is already doing something in this direction. Sure one can have blessed library types that the compiler knows about, but then from language design point of view it creates a decision between those types and others written by developers, but with similar semantics.
- mikeash 10y agoSince you said it's easier than these other languages, I thought you were referring to how the languages work now. There's nothing wrong with special treatment for stuff in the standard library. C and C++ already do this with many things (e.g. many compilers understand calls like memcpy and will emit optimized code based on the standard library semantics) and Swift gives special treatment to built-in types like Optional. The distinction here is fairly blurred. The Swift implementation of weak pointers is mostly in the runtime library already, it's just that the compiler hides it a bit so you don't write the names of the calls directly.
- pjmlp 10y agoSure, I was more thinking about the language geek point of view. Thanks for clarifying.