On Saturday, 9 January 2021 at 02:07:50 UTC, Ali Çehreli wrote:
The destination is immutable(char)[].
No, it's not. string[] means immutable(char)[][] - note the
second set of brackets.
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.
The elements are not immutable. Each element is an mutable slice
of immutable characters.
> Your code has a logical inconsistency
I don't see where. I think this is one of those too strict
cases of D where I unhappily slap a cast and move on.
The cast you propose breaks the type system. For example:
module app;
import std.stdio;
struct Value
{
int value;
string data;
string[] text;
}
void test(const ref Value value) @trusted // Don't do this!
{
Value other = void;
other.text = cast(string[]) value.text; // This cast is
@system for good reason.
other.text[0] = "Oops";
}
void main() @safe
{
immutable(Value) row = {
value: 10,
data: "ttttggg",
text: [ "Don't change me, bro!" ]
};
writeln(row.text);
test(row);
writeln(row.text);
}