const and phobos string functions
Reiner Pope
some at address.com
Thu Aug 2 03:57:20 PDT 2007
Regan Heath wrote:
> 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
I think the code you gave here would be misleading. For instance,
suppose you had your templated overloads, effectively giving you
// copy-on-write
string tolower(string);
// in-place
char[] tolower(char[]);
Then you would get surprising results if you did this:
char[] s = "Hello World!".dup;
int a = howManyLettersDiffer(tolower(s), s);
assert (a == 2); // assert failed, a is actually 0
I think avoiding this is exactly why Kirk suggested his overloads.
-- Reiner
More information about the Digitalmars-d
mailing list