V2 string (general issues)

Walter Bright newshound1 at digitalmars.com
Thu Jul 5 12:11:37 PDT 2007


Kristian Kilpi wrote:
> First, I am wondering why some functions are formed as follows:
> (but I'm sure someone will (hopefully) enlight me about that ;) )
> 
>   string foo(string bar);
> 
> That is, if they return something else than 'bar' (they do some string 
> manipulation).
> Shouldn't they return char[] instead?

No, because then they must always dup the string. If they don't need to 
dup the string, they can return a reference to the parameter, and if so, 
it must be const.

> There should be two different functions, one for each group:
> 
>   char[] tolower(char[] str);  //modifies and returns 'str'
> 
>   char[] getlower(string str);  //returns a copy

When one would use a mutating tolower, one is already manipulating the 
contents of a string character by character. In such cases, one can 
tolower the characters in that process, instead of doing it later (the 
former will be more efficient anyway, and the only advantage to a 
mutating tolower is an efficiency improvement).

Using the functional-style copy-on-write string functions will result in 
easy to understand, less buggy programs. Doing strings in this manner is 
a proven success in just about every programming language.



More information about the Digitalmars-d mailing list