concatenation

Ellery Newcomer ellery-newcomer at utulsa.edu
Wed Jan 26 19:16:53 PST 2011


On 01/24/2011 05:22 PM, Robert Clipsham wrote:
> On 24/01/11 23:09, Ellery Newcomer wrote:
>> in the following:
>>
>> void main(){
>> char[] x;
>> string s;
>> string y;
>>
>> y = s ~ x;
>> }
>>
>> tok.d(5): Error: cannot implicitly convert expression
>> (cast(const(char)[])s ~ x) of type char[] to string
>>
>> why should typeof(s ~ x) == char[] ?
>
> x is a mutable array of mutable chars
> s is a mutable array of immutable chars
>
> If you append something mutable to something immutable, the resulting
> type must be mutable, as some of the contents is mutable and could be
> changed - if that can happen the result can't be immutable. To get
> around this there's .idup I believe.
>

If you append something mutable to something immutable, the resulting 
type can't be mutable, as some of the contents are immutable and may not 
be changed - if you let the result type be mutable you've frivolously 
done away with the type system's protection of the immutable elements.

const(char) makes more sense to me from that front.

char is a value type and x is always going to get copied, so mutability 
isn't really an issue in this case.

number of copy operations might be, though.

y = s ~ x.idup;

is

COPY(s, COPY(x))

as I understand things. Be nice if dmd could flatten the copying.




More information about the Digitalmars-d-learn mailing list