3 ms·
Id argue that c++ is not type safe. Yes, strings could be converted to ints in theory, but these types are obviously not the same, why not make it more explici
by ramon156 2y ago
Id argue that c++ is not type safe.
Yes, strings could be converted to ints in theory, but these types are obviously not the same, why not make it more explicit?
- maleldil 2y agoType safety isn't binary but a spectrum. It's obvious that C++ is more type-safe than JS but less than Rust, which is less than Haskell.
- eru 2y agoWell, at some points it becomes even more complicated than just a spectrum. Your types in different languages just track different things. Eg Haskell's types (normally) don't track lifetimes nor ownership, but Rust does that. In contrast, Haskell likes to track whether side-effects like IO can occur at all, while Rust is happy to just let you eg open a file almost anywhere.
- chuckadams 2y agoHaskell has linear types now, so there's your lifetime and ownership, though thankfully they're optional... wrangling linear types makes Rust look friendly and lenient.
- eru 2y agoNice! I've only used linear types in Clean ages ago, but haven't checked them out in Haskell, yet.
- JonChesterfield 2y agoC++ is obviously hilariously less type safe than JavaScript. JavaScript will convert values from one type to a other at runtime but it's always absolutely sure what the type of the value is. C++ will compile code that looks reasonable, decide it's UB, not tell you about that and proceed to do abject nonsense at runtime. Considering C++ the more "type safe" one of the two is so far from accurate that I wonder if you've mistyped the name of one of the languages.
- eru 2y agoI have a lot of sympathy for your point of view. Though have a look at Rust: it's generally considered much more typesafe than both JavaScript and C++, but it also has (some) dark corners of undefined behaviour with approximately all the same baggage as C++. See also https://news.ycombinator.com/item?id=8206562 https://news.ycombinator.com/item?id=8206562 for a different point of view: dynamically typed languages are equivalent to statically typed languages with just a single static type.
- mofeien 2y agoI agree. C++ is statically typed, as in everything has a type known at compile time. But it is also weakly typed, as in types can be be converted into others implicitly which only makes sense a fraction of the time and readily introduces accidental UB at other times.
- jandrewrogers 2y agoYou can make it both stricter and more explicit. This is a 30 year old API from when C++ was a very different language. In a modern context, this example was explicitly designed to be as loosely checked as it is, it isn't intrinsic. Backward compatibility means they can't change this API to not allow these cases even if it is straightforward to do so.