string literals

Saaa empty at needmail.com
Thu Jan 24 12:26:19 PST 2008


Thanks, so the common use of strings is such that it is best to make the 
default setting 'immutable'.
Then, how about adding the .access property to arrays? (or all types :)
With strings default .access as read only?


>>Why is this? (I expect there to be a nice reason for it, I just down't 
>>know it :)<
>
> That's an interesting question. Now and then I too feel the need of 
> mutable stings, to change their chars, etc, but here Josh Block and the 
> famous Sedgewick explain why immutables are often good:
> http://www.cs.princeton.edu/introcs/33design/
>
> <<
> An immutable data type is a data type such that the value of an object 
> never changes once constructed. Examples: Complex and String. When you 
> pass a String to a method, you don't have to worry about that method 
> changing the sequence of characters in the String. On the other hand, when 
> you pass an array to a method, the method is free to change the elements 
> of the array.
>
> Immutable data types have numerous advantages. they are easier to use, 
> harder to misuse, easier to debug code that uses immutable types, easier 
> to guarantee that the class variables remain in a consistent state (since 
> they never change after construction), no need for copy constructor, are 
> thread-safe, work well as keys in symbol table, don't need to be 
> defensively copied when used as an instance variable in another class. 
> Disadvantage: separate object for each value.
> Josh Block, a Java API architect, advises that "Classes should be 
> immutable unless there's a very good reason to make them mutable....If a 
> class cannot be made immutable, you should still limit its mutability as 
> much as possible."
>>>
>
> Something more:
> - Immutable data works well with multiprocessing
> - it's common in functional style programming allowing more pure functions
> - the GC can manage immutable strings/objects in an efficient enough way.
>
> Bye,
> bearophile 




More information about the Digitalmars-d-learn mailing list