string is rarely useful as a function argument

Timon Gehr timon.gehr at gmx.ch
Fri Dec 30 16:56:47 PST 2011


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. length gives the number of 
code units. operator[i] gives the i-th code unit. Nothing wrong or 
good-for-nothing about that. .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.

Consider:

void foo(ubyte[] b)in{assert(b.length);}body{
     b[0]=2; // perfectly fine
}

void main(){
     char[] s = "☺".dup;
     auto b = s.raw;
     foo(b);
     writeln(s); // oops...
}

I fail to understand why that is desirable.


More information about the Digitalmars-d mailing list