string types: const(char)[] and cstring

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Mon May 28 02:38:39 PDT 2007


Reiner Pope wrote:
> Perhaps I should just wait for the implementation, but I'm interested in 
> knowing what your solution to .dup is. Given
> 
>    auto foo = "hello".dup;
> 
> what is the type of foo?

Most likely a plain (mutable) char[].

> How do you support both of
> 
>    invariant char[] foo = "hello".dup;
>    char[] bar = "hello".dup;

Likely the first will be an error as written, requiring a 
cast(invariant) to be inserted.
Of course, since it doesn't make much sense to .dup in the example above 
("hello" is already invariant, and copying an invariant array but not 
modifying the copy isn't typically useful) that shouldn't be much of a 
problem in this case.

For other cases though, I could see how a "unique" (or similar) type 
constructor that would allow implicit conversion to both mutable and 
invariant (and const) types could be useful.
For instance, if the strings in your example were replaced by mutable 
arrays, a "unique char[]" return value of .dup could then be assigned to 
mutable/const/invariant references without needing casts.



More information about the Digitalmars-d-announce mailing list