5 ms·
> facilities to make correct code easier to write I have read a bit about this for Haskell. What in Rust facilitates the same, that is absent from Go (or C, C+
by bsg75 13y ago
> facilities to make correct code easier to write
I have read a bit about this for Haskell. What in Rust facilitates the same, that is absent from Go (or C, C++) ?
- samnardoni 13y agoType parameterisation, lifetimes, mutability qualifiers. Algebraic data types can be used to avoid null pointers via an Option/Maybe type.
- masklinn 13y agoNon-nullable pointers, a type system which is not full of holes, inability to cast. C and C++ obviously add (remove?) such things as complete lack of memory safety, unchecked out-of-bounds accesses or lifetime management. And Go's inability to assert any immutability is also a strike against correctness (at least C++ has consts, though they're complex beasts)
- PommeDeTerre 13y agoOne thing to consider about C++, though, is that it's powerful enough to already allow you to duplicate or very closely approximate the functionality you listed. This is the essence of what some people call "Modern C++". C++ functionality like references and smart pointers make pointer nullability and memory safety irrelevant, or at least much, much less of an issue than in C or older C++ code. RAII significantly helps with lifetime management of all sorts of resources, from memory to database connections to files. There are various collection libraries for C++ that make out-of-bounds accesses a non-issue, at a very minimal cost. Likewise, it's quite easy in almost all cases to avoid using unsafe casts and other behavior that may cause problems, when taking a more modern approach to C++. With a small amount of discipline, C++ of today can be much different than C++ of the 1980s or 1990s. Many of the pitfalls of that era can be avoided with remarkably little effort, especially when using C++11 and modern implementations. Yet it still easily gives you access to very powerful functionality for those rare cases when it is needed. This is much harder to do in many other, more modern languages.
- steveklabnik 13y agoThere's no question that you _can_ write safe code in C++, but it's very, very hard. > With a small amount of discipline, I would just like to point out that when dynamic language advocates suggest that 'with a small amount of discipline, you can write reasonably safe dynamic code' and you laugh them off with comments about how static typing saves you from errors at compile time. The same is true with languages like Rust or Haskell; you don't need the discipline, the compiler catches such things for you.
- PommeDeTerre 13y agoThe discipline involved with writing safe C++ code is somewhat different from the discipline involved with writing safe code in some dynamic language. In C++, it's mostly just a matter of understanding the language features you're using, and then avoiding ones that can be harmful in some cases, often in favor of much safer alternative approaches. The C++ compiler will still catch many other types of problems at compile time that wouldn't necessarily be noticed (assuming they even are noticed by the developer(s)) until runtime when using a dynamic language. The safety net offered by the C++ compiler may not be cast as wide as that of Rust or Haskell, but it's still there, and it does catch many common issues. And I think it's wrong to say that Rust and Haskell don't require discipline; they do. It's still easy to write inefficient or unmaintainable code in those languages, for example. You still need to know what you're doing, and how to use the functionality that they offer, in order to avoid problems like that. Now, the discipline needed when using a dynamic language not only includes the discipline needed when using C++ (that is, "don't do stupid stuff"), but it also includes not making trivial mistakes like typos, or missing some changes when refactoring, and so on. Much, much more discipline is needed when writing code in dynamic languages. That is the kind of discipline that can be automated away to a very large extent, however. Personally, I do think it's silly and wasteful to willingly take on responsibility for ensuring that sort of discipline for anything but the smallest scripts. So, yes, I will speak out against it and those who suggest it's acceptable, especially in larger systems.
- pcwalton 13y ago
- freyrs3 13y agoRegion-based memory management in Rust makes it possible to deal with shared memory without having to deal with life-cycle maintenance that you deal with in C. C++ has some similar concepts but like most things in C++ they are horribly complicated and rely on large template libraries.