4 ms·
Unfortunately, judging by the fact so many people are still confused about it, it was a mistake. Having a shorthand for something (str) and that thing (String)
by sirwhinesalot 4y ago
Unfortunately, judging by the fact so many people are still confused about it, it was a mistake. Having a shorthand for something (str) and that thing (String) be different things was dumb, and someone brought that up in the discussion at the time but I guess hindsight is 20/20.
C++ has std::string and std::string_view which makes a loads more sense.
Java and C# have StringBuilder and String.
Go has strings.Builder and string.
Objective-C/Cocoa has and NSMutableString and NSString.
ADA has Unbounded_String, Bounded_String and Fixed_String for different use cases.
Rust has by far the worst naming.
- kzrdude 4y agoI guess C++ has the best names after all, Rust should have emulated those (except it couldn't - string_view came after Rust and maybe even was inspired by Rust.)
- cmrdporcupine 4y agoChromium's C++ StringPiece dates back to at least 2012, and pretty sure Google had something similar (I forget the original name) to it in Google3's C++ base library which became abseil's string_view before that even. And Boost had string_ref at least as far back as 2013. https://chromium.googlesource.com/chromium/src/base/+/master/strings/string_piece.h https://chromium.googlesource.com/chromium/src/base/+/master... https://github.com/abseil/abseil-cpp/blob/master/absl/strings/string_view.h https://github.com/abseil/abseil-cpp/blob/master/absl/string... https://github.com/steinwurf/boost/commits/master/boost/utility/string_ref.hpp https://github.com/steinwurf/boost/commits/master/boost/util... All of this is arguably before Rust's influence would have been anything to speak of.
- berkut 4y agoGoogle had StringPiece and LLVM had StringRef in 2012 and provided similar functionality. https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3442.html https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n34... No mention of Rust there I can see.
- oconnor663 4y agoIt has downsides, but it also has upsides. Calling it "String" doesn't tell you much about what exactly it does or how it relates to other types, but it does tell you "hey if you want to read and write text then you need to learn about this type". If it was called StrBuf or something, I might worry about new learners seeing that name and assuming it was some advanced concept they don't need yet.
- astrange 4y ago> Objective-C/Cocoa has NSMutableString and NSString. This (or value typed strings) is an important optimization that most other systems don't properly have. If you know the string is immutable, so it won't ever have to be resized, you can do a single allocation (string+buffer) instead of having an extra pointer dereference (string->buffer).
- afdbcreid 4y agoI don't know Objective-C/Cocoa, but is it when you know the size at compile time? If yes, you can use an array.
- astrange 4y ago“At compile time” is too strict; that’s a constant string. And an array isn’t a string, it’s an array. (A string is only a byte array because everything is a byte array.)
- ratmice 4y agoNo it is when you know the size during runtime, or at time of construction. there is a 3rd class for constant strings known at compile time but it is mostly hidden behind @"" compiler syntax.
- astrange 4y agoNS(Mutable)Strings aren’t “classes” but “class clusters”. Making them classes would be an inappropriate mix of interface and implementation. Of course, despite it being a bad idea, almost all other data structure library does do it that way. Constant strings have the same memory layout as other strings more-or-less but have different layouts for ASCII and Unicode, and in memory immutable strings have different backing classes for tagged pointer storage and for UNIX paths.