string is rarely useful as a function argument

Timon Gehr timon.gehr at gmx.ch
Sat Dec 31 08:13:36 PST 2011


On 12/31/2011 01:15 PM, Don wrote:
> On 31.12.2011 01:56, Timon Gehr wrote:
>> On 12/31/2011 01:12 AM, Andrei Alexandrescu wrote:
>>> On 12/30/11 6:07 PM, Timon Gehr wrote:
>>>> alias std.string.representation raw;
>>>
>>> I meant your implementation is incomplete.
>>
>> It was more a sketch than an implementation. It is not even type safe
>> :o).
>>
>>>
>>> But the main point is that presence of representation/raw is not the
>>> issue.
>>> The availability of good-for-nothing .length and operator[] are
>>> the issue. Putting in place the convention of using .raw is hardly
>>> useful within the context.
>>>
>>
>> D strings are arrays. An array without .length and operator[] is close
>> to being good for nothing. The language specification is quite clear
>> about the fact that e.g. char is not a character but an utf-8 code unit.
>> Therefore char[] is an array of code units.
>
> No, it isn't. That's the problem. char[] is not an array of char.
> It has an additional invariant: it is a UTF8 string. If you randomly
> change elements, the invariant is violated.

char[] is an array of char and the additional invariant is not enforced 
by the language.

>
> In reality, char[] and wchar[] are compressed forms of dstring.
>
>> .raw would return ubyte[], therefore it
>> would lose all type information. Effectively, what .raw does is a type
>> cast that will let code point data alias with integral data.
>
> Exactly. It's just a "I know what I'm doing" signal.

No, it is a "I don't know what I'm doing" signal: ubyte[] does not carry 
any sign of an additional invariant, and the aliasing can be used to 
break the invariant that is commonly assumed for char[]. That was my point.


More information about the Digitalmars-d mailing list