string is rarely useful as a function argument

Ali Çehreli acehreli at yahoo.com
Wed Dec 28 08:00:03 PST 2011


On 12/28/2011 04:06 AM, Peter Alexander wrote:
 > string is immutable(char)[]
 >
 > I rarely *ever* need an immutable string. What I usually need is
 > const(char)[]. I'd say 99%+ of the time I need only a const string.
 >
 > This is quite irritating because "string" is the most convenient and
 > intuitive thing to type. I often get into situations where I've written
 > a function that takes a string, and then I can't call it because all I
 > have is a char[]. I could copy the char[] into a new string, but that's
 > expensive, and I'd rather I could just call the function.
 >
 > I think it's telling that most Phobos functions use 'const(char)[]' or
 > 'in char[]' instead of 'string' for their arguments. The ones that use
 > 'string' are usually using it unnecessarily and should be fixed to use
 > const(char)[].
 >
 > In an ideal world I'd much prefer if string was an alias for
 > const(char)[], but string literals were immutable(char)[]. It would
 > require a little more effort when dealing with concurrency, but that's a
 > price I would be willing to pay to make the string alias useful in
 > function parameters.

Agreed. I've talked about this in D.learn a number of times myself.

Ali



More information about the Digitalmars-d mailing list