Immutability and strings

Steven Schveighoffer schveiguy at yahoo.com
Mon Aug 23 04:41:46 PDT 2010


On Wed, 18 Aug 2010 22:11:35 -0400, Andrej Mitrovic  
<andrej.mitrovich at 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


More information about the Digitalmars-d mailing list