Confused about String mutability #18482
Unanswered
rcliftonharvey
asked this question in
Questions and Answers
Replies: 1 comment 6 replies
-
The thing you're missing is that adding 2 strings together, as in your first example, creates a third string and assigns it to If you attempt to modify the contents of the string using I agree it can be a bit confusing at first. |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Forgive me if it's a silly question, or if it's been discussed before, I did look.
But... why are mutable immutable Strings immutable and not mutable?
Yeah, OK. Let me explain...
At first, the V documentation states that Strings are immutable.
After stating this absolute rule, just a few lines further down in the documentation, an example discounts this absolute rule by just assigning a new value to an existing String, without any further comment or explanation to the example or what's happening.
So clearly, by applying the "mut" keyword to the declaration, Strings CAN very much be made mutable, and clearly the absolute rule of String immutability is wrong. (They're just immutable upon declaration without "mut", like any other variable.)
Now if Strings are mutable, then why can the content of a mutable String not be changed with the [] access operator?
Why does this give me an error? This behaviour is totally counter-intuitive and confusing.
If the String had been declared without the "mut" keyword, i.e. as a regular immutable String, then I would have no objections. But to tell me that a clearly mutable String can not be altered because it's immutable... that just doesn't make any sense whatsoever.
I mean, I can (probably) understand the programming/implementation side of it. I guess the [] access operator returns a const reference, which explains why the String can't be written to - but not, why this would be the case.
And if a String marked as mutable can not be altered with the [] access operator... then why can it be altered via += concatenation, which is (from the outside) no different than assigning a different value, i.e.
str1 += str2
==str1 = str1+str2
?For comparison, in a C++ std::string, the [] access operator of a non-const std::string instance, as well as the .at() access method, always give write access to the underlying String data. Only if the String is marked as const, will assignment through these operators/methods fail.
This C++ way is a totally logical concept. A non-const string can be altered, a const string can not be altered.
I can understand that variables in V are automatically const/immutable on declaration, unless the "mut" keyword is specified. I can wrap my head around that and code accordingly.
But my OCD can not accept that a variable that's clearly marked as mutable can NOT be altered. This is illogical and inconsistent behaviour, and I think this should be improved for consistency and logic.
Or am I just missing something too obvious?
Cheers
Beta Was this translation helpful? Give feedback.
All reactions