Another prayer for invariant strngs

Robert Fraser fraserofthenight at gmail.com
Thu Jul 12 13:12:05 PDT 2007


Invariant strings have been discussed before (briefly) in discussions of constness, however I wish to bring up the subject again more directly.

The "string" alias as it is now (in D 2.0) is an odd beast. The problem is that it is invariant(char)[] instead of invariant(char[]), so that while the characters themselves are invariant, the array is mutable. This has two main problems:

1. It's confusing. There have been quite a few topics both in this newsgroup and in digitalmars.D.learn about how exactly to use the 2.0 string alias and where it's immutable/where it's not.

2. Performance. While writing my own code, I can pretend "string" is invariant (or use my own invariant(char[]) alias), but when passing to, or receiving code from library functions, this is not possible. This means that in each of these situations I must take two, performance-draining precautionary measures:
i. Duplicate the string every time it's passed in or out of my code.
ii.Synchronize multithreaded access to strings/acquire locks/etc.

Invariant strings have precedent: they're used in Java, .NET, Perl, Python, Ruby and quite a few other languages. And for when multiple string operations are going down, there's always char[] and .idup to fall back on, which are far better than Java's StringBuffer, etc.

So, please, Walter... consider Andrei's proposal and make "string" an alias to invariant(char[]). It'll make a lot of happiness happen.



More information about the Digitalmars-d mailing list