www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Where are the strings in std.string?

reply "TheManWithNoName" <kheaser eapl.org> writes:
I don't mean to be splitting hairs here, well maybe I do, but

D clearly makes the string type to be immutable(char)[].  That's 
fine, but the std.string is all about char[] or mutable character 
arrays.

So now, whenever I see a reference to the string, I have to ask 
myself: do they mean the formal definition of string, 
immutable(char)[], or the colloquial version, char[].
Oct 27 2012
parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sat, Oct 27, 2012 at 06:53:56PM +0200, TheManWithNoName wrote:
 I don't mean to be splitting hairs here, well maybe I do, but
 
 D clearly makes the string type to be immutable(char)[].  That's
 fine, but the std.string is all about char[] or mutable character
 arrays.

Huh? I think you're confusing const(char)[] for char[]. In D, both T[] and immutable(T)[] for any type T can be implicitly converted to const(T)[]. The reason many of the functions in std.string take const(T)[] is because they work with *both* string and char[]. There is also a bunch of template functions that take C[] for any character type C. This means it can take char[], const(char)[], string, wchar[], wstring, dchar[], dstring, etc.. These are all generic functions. Most of these functions don't require mutable strings at all.
 So now, whenever I see a reference to the string, I have to ask
 myself: do they mean the formal definition of string,
 immutable(char)[], or the colloquial version, char[].

There is no colloquial version of string, a string in D is always immutable(char)[]. char[] is called a character array. T -- It is widely believed that reinventing the wheel is a waste of time; but I disagree: without wheel reinventers, we would be still be stuck with wooden horse-cart wheels.
Oct 27 2012