Implicit conversion of concatenation result to immutable
Per Nordlöw
per.nordlow at gmail.com
Thu Apr 1 21:21:02 UTC 2021
Can somebody explain the logic behind the compiler disallowing
both line 3 and 4 in
```d
const(char)[] x;
string y;
string z1 = x ~ y; // errors
string z2 = y ~ x; // errors
```
erroring as
```
Error: cannot implicitly convert expression `x ~
cast(const(char)[])y` of type `char[]` to `string`
Error: cannot implicitly convert expression `cast(const(char)[])y
~ x` of type `char[]` to `string
````
Has this something to do with the compiler being defensive about
possible in-place appending or prepending to the arguments (in
this case `x` and `y`) passed to the array concatenation
expression?
For instance, could `x ~ y` return either
- a back-extended slice `x[0 .. x.length + y.length]` with `y`
appended at the back or
- a front-extended slice `y[-x.length .. y.length]` with `x`
prepended to the front
provided the GC has information about available free memory there?
This problem regularly crops up for me during assembling of
strings passed as a string parameter for instance an exception
constructor.
More information about the Digitalmars-d
mailing list