string types: const(char)[] and cstring

Reiner Pope some at address.com
Tue May 29 03:36:57 PDT 2007


Frits van Bommel wrote:
> 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.
Funny, that's just what I thought of (including the name unique). When I 
  first thought about it, I thought that such a construct would be very 
useful and very powerful, but I can't actually think of any use cases 
except with .dup and other constructor-type functions. (Although 
supporting them should alone be enough motivation).



More information about the Digitalmars-d-announce mailing list