4 ms·
I agree on the former two (std::string and smart pointers) because they can't be nicely implemented without some help from the language itself. The latter two
by teo_zero 8mo ago
I agree on the former two (std::string and smart pointers) because they can't be nicely implemented without some help from the language itself.
The latter two (hash maps and vectors), though, are just compound data types that can be built on top of standard C. All it would need is to agree on a new common library, more modern than the one designed in the 70s.
- uecker 8mo agowhy not std::string?
- direwolf20 8mo agoIt's a class, so it doesn't work in C.
- uecker 8mo agoSure, but you can have a similar string abstraction in C. What would you miss? The overloaded operators?
- direwolf20 8mo agoAutomatic memory accounting — construct/copy/destruct. You can't abstract these in C. You always have to call i_copied_the_string(&string) after copying the string and you always have to call the_string_is_out_of_scope_now(&string) just before it goes out of scope
- uecker 8mo agoThis seems orthogonal to std::string. People who pick C do not want automatic memory management, but might want better strings.
- direwolf20 8mo agoAutomatic memory management is literally what makes them better
- uecker 8mo agoFor many string operations such as appending, inserting, overwriting etc. the memory management can be made automatic as well in C, and I think this is the main advantage. Just automatic free at scope end does not work (without extensions).
- direwolf20 8mo agoYou can make strings (or bignums or matrices) more convenient than the C default but you can never make them as convenient as ints, while in C++ you can.
- uecker 8mo agoYes, but I do not think this is a good thing. A programming language has to fulfill many requirements, and convenience for the programmer is not the most important.
- direwolf20 8mo agoEmpirically it is. All the most used languages are the most convenient ones.
- teo_zero 8mo agoYou can surely create a std::string-like type in C, call it "newstring", and write functions that accept and return newstrings, and re-implement the whole standard library to work with newstrings, from printf() onwards. But you'll never have the comfort of newstring literals. The nice syntax with quotes is tied to zero-terminated strings. Of course you can litter your code with preprocessor macros, but it's inelegant and brittle.
- krapp 8mo agoHacker News seems not to hate antirez's sds https://github.com/antirez/sds https://github.com/antirez/sds https://news.ycombinator.com/item?id=45014911 https://news.ycombinator.com/item?id=45014911
- pjmlp 8mo agoIf only WG14 added something similar to C. Yes, SDS exists, however vocabulary types are quite relevant for adoption at scale.
- tialaramex 8mo agoBecause C wants to run on bare metal, an allocating type like C++ std::string (or Rust's String) isn't affordable for what you mean here. I think you want the string slice reference type, what C++ called std::string_view and Rust calls &str. This type is just two facts about some text, where it is in memory and how long it is (or equivalently where it ends, storing the length is often in practice slightly faster in real machines so if you're making a new one do that) In C++ this is maybe non-obvious because it took until 2020 for C++ to get this type - WG21 are crazy, but this is the type you actually want as a fundamental, not an allocating type like std::string. Alternatively, if you're not yet ready to accept that all text should use UTF-8 encoding, -- and maybe C isn't ready for that yet - you don't want this type you just want byte slice references, Rust's &[u8] or C++ std::span<char>
- ninkendo 8mo agoI think a vec is important for the same reason a string is… because being able to properly get the length, and standardized ways to push/pop from them that don’t require manual bounds checking and calls to realloc. Hash maps are mostly only important because everyone ought to standardize on a way of hashing keys. But I suppose they can both be “bring your own”… to me it’s more that these types are so fundamental and so “table stakes” that having one base implementation of them guaranteed by the language’s standard lib is important.