string is rarely useful as a function argument

Peter Alexander peter.alexander.au at gmail.com
Wed Dec 28 04:06:06 PST 2011


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.


More information about the Digitalmars-d mailing list