Cannot implicitly convert expression of type const(string[]) to string[]

Paul Backus snarwin at gmail.com
Sat Jan 9 02:56:49 UTC 2021


On Saturday, 9 January 2021 at 02:07:50 UTC, Ali Çehreli wrote:
> On 1/8/21 3:10 PM, tsbockman wrote:
>
> > On Friday, 8 January 2021 at 20:43:37 UTC, Andrey wrote:
> >> Hello,
> >>
> >>> struct Value
> >>> {
> >>>     int value;
> >>>     string data;
> >>>     string[] text;
>
> The destination is immutable(char)[]. The characters cannot be 
> changed. We can still append but other slices that share the 
> same data is protected by D's no-stomp decision.
>
> >>> }
> >>>
> >>> void test(const ref Value value)
> >>> {
> >>>     Value other = void;
> >>>     other.text = value.text;
>
> Even though the source is 'const ref', other.text is a copy of 
> the slice object (the pointer and the length). Because the 
> elements are immutable, other.text cannot mutate value.text.

Remember, `string[]` means `immutable(char)[][]`, so there are 
actually two layers of pointer + length here. The outer one is 
copied, but the inner one is not, which means that mutating 
`other.text[0]` would also mutate `value.text[0]` if this 
assignment were allowed.


More information about the Digitalmars-d-learn mailing list