const and phobos string functions

Regan Heath regan at netmail.co.nz
Thu Aug 2 03:39:35 PDT 2007


Kirk McDonald wrote:
> In this particular case, you could call this mutating form of tolower() 
> on the buffer returned from read(), and then split it afterwards 
> (perhaps yielding strings). This makes a certain degree of logical 
> sense: If you'd wanted to keep the original contents of the buffer, 
> you'd have to duplicate it at some point anyway. Since you don't, you 
> can alter it immediately.

Sure, that solves this particular case.  But, do you agree there is 
generally a problem, or shall I continue to dream up example cases for 
you to solve in some other manner :)

> I submit the following Python idiom: Functions which mutate their 
> arguments should return nothing. That is:
> 
> // Return new string
> string tolower(string);
> // Mutate argument
> void tolower(char[]);
> 
> This rather strictly highlights the difference between char[] and 
> string, and makes it essentially impossible to mix up library functions 
> differentiated in this way.

That's all well and good until you want to write:

char[] s;
...
foo(tolower(s));

If you template tolower in the manner I described it's not possible to 
mix it up and call the wrong one anyway (as it selects the correct one 
based on the input) so it's a non-problem.

Regan



More information about the Digitalmars-d mailing list