www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Immutability and strings

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Here's something from TDPL, page 292:

string process(string input) { return input; };

string s1 = "blah";
string s2 = process(s1);
assert(s1 == "blah");   // never fails

I've added the return in process(), it wasn't there. Andrei states that it's
impossible for s2 to be changed after the assignment. And that the code above
never fails.

I agree that in this case s1 and s2 will remain the same. But this has to do
*not just* with immutability, but with the fact that s1 is passed by value. I
think that should be mentioned here.

Look what happens if I make the string a reference parameter, and do some
innocent appending:

string process(ref string input) 
{
    input ~= "lala";
    return input; 
};

void main()
{
    string s1 = "blah";
    string s2 = process(s1);
    assert(s1 == "blah");   // fails
}

It's just nitpicking, I know. And I'm sure Andrei was specifically documenting
only that first example. But I do think it was worth mentioning that
immutability on individual elements isn't a safe bet unless you're sure the
function does not take a reference parameter.
Aug 18 2010
next sibling parent Jonathan M Davis <jmdavisprog gmail.com> writes:
On Wednesday, August 18, 2010 19:11:35 Andrej Mitrovic wrote:
 It's just nitpicking, I know. And I'm sure Andrei was specifically
 documenting only that first example. But I do think it was worth
 mentioning that immutability on individual elements isn't a safe bet
 unless you're sure the function does not take a reference parameter.
Well, I thought that that was pretty much a given, but I guess I can see how someone could miss that. But really, that's what ref _does_. You can append to strings all you like in your own function. Why would expect that not to be possible once it's been passed by ref? ref makes it free game to mess with what you pass it. So, I guess I can see how someone might miss this if they're thinking of strings being totally immutable, but it's pretty basic. Whatever you can do to your variable in your function can be done to in a function that takes that variable by ref. - Jonathna M Davis
Aug 18 2010
prev sibling next sibling parent Kagamin <spam here.lot> writes:
Andrej Mitrovic Wrote:

 I agree that in this case s1 and s2 will remain the same. But this has to do
*not just* with immutability, but with the fact that s1 is passed by value. I
think that should be mentioned here.
 
In the first example a string reference is passed by value. In the second example a string reference is passed by reference.
Aug 18 2010
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Wed, 18 Aug 2010 22:11:35 -0400, Andrej Mitrovic  
<andrej.mitrovich gmail.com> wrote:

 Here's something from TDPL, page 292:

 string process(string input) { return input; };

 string s1 = "blah";
 string s2 = process(s1);
 assert(s1 == "blah");   // never fails

 I've added the return in process(), it wasn't there. Andrei states that  
 it's impossible for s2 to be changed after the assignment. And that the  
 code above never fails.
Well, it's not a very good example if you have the implementation be return input; Better to make it {...} to denote that any code is possible inside the {}, and the assert is still guaranteed to pass. Note, I don't have TDPL, so I don't know what was originally there...
 I agree that in this case s1 and s2 will remain the same. But this has  
 to do *not just* with immutability, but with the fact that s1 is passed  
 by value. I think that should be mentioned here.
I agree, that would be a good thing to mention. -Steve
Aug 23 2010