string is rarely useful as a function argument

Walter Bright newshound2 at digitalmars.com
Wed Dec 28 10:15:28 PST 2011


On 12/28/2011 10:07 AM, Peter Alexander wrote:
> On 28/12/11 5:16 PM, Walter Bright wrote:
>> On 12/28/2011 5:16 AM, Peter Alexander wrote:
>>> Any time you want to create a string without allocating memory.
>>>
>>> char[N] buffer;
>>> // write into buffer
>>> // try to use buffer as string
>>
>> Is the buffer ever going to be reused with a different string in it?
>
> Possibly.
>
> I know what argument is coming next: "But if the function you call stores the
> string you passed in then it can't rely on seeing a consistent value!"

Exactly.


> I know this. These functions should request immutable(char)[] because that's
> what they need. Functions that don't store the string should use const(char)[].
>
> The question is whether string should alias immutable(char)[] or const(char)[].
> In my experience (which is echoed in Phobos) is that const(char)[] is used much
> more often than immutable(char)[], so it should alias const(char)[].

If such a change is made, then people will use const string when they mean 
immutable, and the values underneath are not guaranteed to be consistent.


More information about the Digitalmars-d mailing list