string is rarely useful as a function argument

Timon Gehr timon.gehr at gmx.ch
Wed Dec 28 10:03:35 PST 2011


On 12/28/2011 07:07 PM, 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!"
>
> 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)[].

You are approximately saying (paraphrasing): "The question is whether a 
cow is a cow or an animal. In my experience (which is echoed at the farm 
down the valley) is that there are more animals than there are cows. So 
we should call all our animals cows."


More information about the Digitalmars-d mailing list