const?? When and why? This is ugly!

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon Mar 9 05:58:12 PDT 2009


Sergey Gromov wrote:
> Sun, 08 Mar 2009 19:24:32 -0700, Andrei Alexandrescu wrote:
> 
>> As far as signatures of functions in std.string, I agree that those not 
>> needing a string of immutable characters should just accept in Char[] 
>> (where Char is one of the three character types). That should make 
>> people using mutable and immutable strings equally joyous.
> 
> So you agree that *the standard library* should avoid using immutable
> when it's not strictly necessary after all.

I do.

> This is quite in contrary
> to what Walter says.

It is. This may be because of a slight difference in philosophy; I think 
any entity (function, type) of the standard library should accept the 
most general types it could conceivably work with. In fact I'd be happy 
put all or most significant algorithms in std.string inside std.algorithm.

   Now the question is, how any *other* libraries
> differ?  I usually don't care about how you use immutable in your
> personal code.  But if there is an XML library which takes immutables
> everywhere,  it seriously limits my freedom in chosing my coding style.

I agree but disagree with "seriously".

> Immutable string manipulation produces tons of garbage objects.

This I disagree with. There may be extra copies, but you also save other 
copies. Also there's never risky aliasing - coding with immutable 
strings is safe. D simply can't afford to seriously suggest in this day 
and age a programming style with aliased mutable strings.

> Every
> time you want to replace '\\' with '/' you get garbage, if not as many
> times as you encounter '\\' in the string.  This is something we avoided
> at any cost in mobile Java games.  This is the attitude which makes
> "large" Java applications so memory-hungry.  I have a hard time to
> believe this magically became OK in D.

It does become OK (albeit not magically) in D because D does offer you 
the option to work with char[] if you need to. I understand that the 
problem of you calling into libraries only taking string still remains, 
but really you can't have everything. I think we're in better shape than 
most languages.


Andrei



More information about the Digitalmars-d mailing list