Is all this Invarient **** er... stuff, premature optimisation?

Bruno Medeiros brunodomedeiros+spam at com.gmail
Tue Apr 29 16:26:54 PDT 2008


Walter Bright wrote:
> Steven Schveighoffer wrote:
>> I agree that immutable strings can be valuable.  That's why I think 
>> it's important to have a version of toupper that uses invariant 
>> strings because you can make more assumptions about when to make 
>> copies.  But why shouldn't there be a version that does the same thing 
>> with mutable or const strings? Why should a developer be forced to 
>> always use invariant strings when the optimizations and multithreading 
>> benefits that come with only using invariant strings may not be more 
>> important for a particular program than being able to modify a 
>> string?  I should still be able to use toupper on mutable strings as 
>> well...
> 
> That's why I agreed with Janice on making a stringbuffer module that 
> operates on mutable strings. It's easier than arguing about it, and it 
> doesn't hurt to have such a package. And I suspect that after using it 
> for a while, people will naturally evolve towards using all invariant 
> strings.

"people will naturally evolve towards using all invariant strings."
Oh please. This whole discussion between "Me here" and Walter was always 
occurring under the notion that one either has to use all mutable 
strings, or all invariant strings, which is a silly idea. Use what is 
right for what you are trying to do!

The original post code was a clear-cut example of invariant misuse. If 
you are going to make one or several different mutations to a string, do 
not use invariant, use mutable. The fact that there isn't a 
mutable/in-place tolower has no bearing on the const/invariant system 
(only on the Phobos library design). So if you had any quarrel, it 
wasn't with D's immutability system, but with library design (which 
Walter already said he plans to fix... at least on what std.string is 
concerned).

And Walter, people won't "naturally evolve towards using all invariant 
strings" (nor they should). If I have a function where I'm going to 
perform a series of changes to a string, I'm not going to dup them with 
each change just to say "How cute, I'm using invariant all the way!". 
I'll do all the changes on a mutable string, and they return either a 
mutable, const, or invariant string, as appropriate to what makes sense 
in the code.

-- 
Bruno Medeiros - Software Developer, MSc. in CS/E graduate
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d mailing list