6 ms·
This all seems like a mis-feature. Smells of C++.
by TylerE 13y ago
This all seems like a mis-feature. Smells of C++.
- lifthrasiir 13y agoYou can make a similar table with C++, complete with pointer, pointer-to-member, function pointer, member function pointer, reference, r-value reference, array, std::auto_ptr, std::unique_ptr, std::shared_ptr, const and volatile. :)
- hdevalence 13y agoExcept C++ doesn't have, for instance, a way to mark data as immutable, so the table's going to be a bit lacking...
- Tyr42 13y agoErr, I'm a big fan of Rust and all, but I'm pretty sure c++ does have const and so on. What c++ lacks is a difference between owned and borrowed pointers, except by convention and so on.
- bzbarsky 13y agoC++ const doesn't actually provide constness for purposes of concurrent access unless you follow a bunch of other rules (no const_cast, no use of "mutable", no use of mutable class statics, no use of mutable file-level statics, no use of mutable globals) that in practice people violate all the time even with const objects.
- hdevalence 13y agoAh, but C++ "const" doesn't do what it says on the tin! What "const" means is not "constant", but "read-only". Something that's const to you might not be const to something else, so you can never depend on it staying the same. I may be wrong, but my understanding is that Rust's constants are actually constant, and proved as such by the compiler, which is a major difference over C++.
- lifthrasiir 13y agoRust's "immutable" references ensure that it cannot be changed from either the reference holder or other safe code that has a reference to the same memory region. Note that (as the periodic table suggests) "mutable" references can be downgraded to the immutable references, but the mutable references are locked while the immutable references are active. Once the immutable references are gone (this is checked by the compiler, see the lifetime guide for details) the mutable references can be used again.
- pcwalton 13y agoThat's right. If you have an `&` reference to something, the language enforces that it will never be mutated as long as that reference is alive. (Also, if you have an `&mut` reference to something, the language enforces that you're the only one who can mutate it while that reference is alive; that's how iterator invalidation is prevented.)
- haberman 13y agoI've often wished for this in C and C++, particularly from an optimization perspective. I always hated that the optimizer cannot assume that the the contents of a struct to which I have a const pointer won't change out from under me when I call another function. It means that it cannot cache members of this struct in callee-save registers; it has to reload them every time. I don't know how much of a speed difference it would actually make in practice, but it bothers me that I cannot express this in C++.
- renox 13y ago> Ah, but C++ "const" doesn't do what it says on the tin! What "const" means is not "constant", but "read-only". Yes and that's a huge trap, which is why I'm so annoyed that D has copied 'const' from C++ instead of renaming it to 'view' or 'read'..
- scott_s 13y agoNot only is it "read only," it's not even necessarily read only: #include <iostream> void f(const int* var) { *var = 42; } int main() { int* ptr = new int(78); f(ptr); std::cout << *ptr << std::endl; return 0; } Compiling produces: [scott_s@local Code] g++ const.cpp const.cpp: In function ‘void f(const int*)’: const.cpp:5: error: assignment of read-only location But if we make it: #include <iostream> void f(const int* var) { int* sneaky = const_cast<int*>(var); *sneaky = 42; } int main() { int* ptr = new int(78); f(ptr); std::cout << *ptr << std::endl; return 0; } We get: [scott_s@local Code] g++ const.cpp [scott_s@local Code] ./a.out 42 C++ gives us escape hatches all over the place. I think that the modern approach of compartmentalizing all escape hatches into explicitly regions of "unsafe" code, and providing no escape hatches outside of such regions, is much better.
- bascule 13y agoIt's actually correcting the mistakes of C++: - Ownership: means no unsafely shared mutable state, checked at compile time with no runtime overhead. Also, no explicit deallocations! - A real type system: Rust's type system is inspired by the Hindley-Milner type inference algorithm used in languages like ML and Haskell
- TylerE 13y agoThe thing is I've used lots of languages with full H-M inference that are much less complicated than this.
- olsonjeffery 13y ago> lots of languages with full H-M inference These are the ones that come to my mind: - ML family (Standard ML, Caml, OCaml, F#) - Haskell None of these are competing in the same space as Rust: fine-grained memory control (read: perf on par w/ C++) with zero-cost abstractions for safety. You can certainly argue that all of those languages are as safe as Rust, with the lack of nulls and explicit mutability (taken to a new level in Haskel), but you can't say they expose a memory models that actually reflects the underlying system (and are as tunable) to the extent that Rust does.
- tel 13y agoRust implements a kind of region typing or linear logic, too, right? That's significantly beyond anything you'll see in garden variety Haskell/ML (though you can embed linear logic in the Haskell type-class machinery with a final encoding [0]). [0] http://okmij.org/ftp/tagless-final/course/#linear http://okmij.org/ftp/tagless-final/course/#linear
- pcwalton 13y agoYes to both. Lifetimes are basically regions, and unique types are basically affine types. (We usually stick to the C++ terminology, though, for familiarity's sake.)
- pcwalton 13y agoCould you elaborate as to what you'd prefer instead?
- TylerE 13y agoA simpler language.
- dbaupp 13y agoAnd how do you propose to get a simpler* language with equal control and safety? *I actually think Rust is relatively simple: once one "gets" ownership, then most of the other hard things follow from it, including lifetimes.
- TylerE 13y agoLook at nimrod (http://www.nimrod-lang.org http://www.nimrod-lang.org).
- dbaupp 13y agoIt doesn't satisfy the safety or control criteria, e.g. it has a compulsory GC.
- pcwalton 13y agoNimrod is GC'd (reference counting is a GC, and DRC has pauses), or unsafe if you don't use GC. It's not in quite the same space as Rust.
- deleted 13y ago[deleted]
- girvo 13y agoI'm a massive Nimrod fan, but those safety guarantees are void once you use manual memory management. What Rust gives you is safety with memory management. Nimrods GC and type system are both really powerful, but Araq and I were chatting about Rusts type system on irc for a reason...
- deleted 13y ago[deleted]