This is BS, You do not promise anything to the compiler you instruct the compiler how to handle the data,
You already found the documentation, in fact you quoted the docs
Remark: Note that specifying const is a contract between the programmer and the compiler.
It is the programmer who tells the compiler that the contents of the const parameter will not be changed
when the routine is executed, it is not the compiler who tells the programmer that the parameter will not be changed.
Of course I found he documentation I like to be aware of any pitfalls I might encounter didn't quoted though. Lets just say that I strongly disagree with that sentence as well the proper statement should be..
It is the programmer who tells the compiler that the contents of the const parameter
should not be changed.
That means that the compiler is responsible for enforcing that instruction if it does not then there is no point in having it.
Take for example
// code assumes {$H+} or string = ansistring;
const
MyText : String = 'Hello ';
var
Txt : string;
begin
Txt := MyText;
Txt := Txt+'Wooooooooow';
end;
would you expect the above code to change the data of MyText constant?
No, but
Txt := Txt+'Wooooooooow';
Is making a copy.
Strings are documented to do copy-on-write, if the ref count is bigger than 1
Txt := MyText;
Increased the ref count of the string.
Exactly my point the behavior is counter intuitive for a const parameter and its not the same on all the types making a small by bothersome exception, I would expect that behavior on a var parameter though.
Lets take
program Project1;
var s,t: string;
begin
s := 'foo-bar';
t := s;
pchar(@s[4])^ := '+';
writeln(s);
writeln(s);
end.
and it does change both.
On that piece of code you intentionally bypassed the fcl string handlers and with that you claim the responsibility that you know what you are doing. It is the same concept with type casting a TObject variable to a TComponent or some other class higher or the hierarchy. Isn't it?
In short I am aware of why that happens I'm just against the current behavior for the above reasons. Is this going to change? I doubt it the dice have fallen and there are code lines out there that depend on this behavior so if I was in charge of that decision I would be against it at this point in time, doesn't make it right though.