string is rarely useful as a function argument

Jonathan M Davis jmdavisProg at gmx.com
Thu Dec 29 14:05:15 PST 2011


On Thursday, December 29, 2011 11:32:52 Sean Kelly wrote:
> Don't we already have String-like support with ranges?  I'm not sure I
> understand the point in having special behavior for char arrays.

To avoid common misusage. It's way to easy to misuse the length property on 
narrow strings. Programmers shouldn't be using the length property on narrow 
strings unless they know what they're doing, but it's likely the first thing 
that any programmer is going to use for the length of a string, because that's 
how arrays in general work.

If it weren't legal to simply use the length property of a char[] or to 
directly slice it or index it, then those common misuages would be harder to 
do. You could still do them via .rep or .raw or whatever we'd call it, but it 
would no longer be the path of least resistance.

Yes, Phobos may avoid the issue, because for the most part its developers 
understand the issues, but many programmers who do not understand them, will 
make mistakes in their own code which should arguably be harder to make, 
simply because it's the path of least resistance, and they don't know any 
better.

- Jonathan M Davis


More information about the Digitalmars-d mailing list