[fpc-devel] Minor doc error about "const param"

Martin Frb lazarus at mfriebe.de
Fri Dec 24 19:35:10 CET 2021


On 24/12/2021 18:07, Bart via fpc-devel wrote:
> On Fri, Dec 24, 2021 at 12:00 PM Adriaan van Os via fpc-devel
> <fpc-devel at lists.freepascal.org> wrote:
>
>> Neither would the compiler allow (I hope) the const parameter to be passed as a var parameter.
> It does not:
>
> {$mode objfpc}
> {$h+}
>
> procedure foo(var x: integer);
> begin
> end;
>
> procedure bar(const y: integer);
> begin
>    foo(y); //line 10
> end;
>

The first quote was probably a ref to the (current) case from the forum, 
that triggered this.

procedure Foo(const A: String; var B: string);
begin ... end;

var s: string;
Foo(s,s);

It is most likely (but not certain) that B will be modified. And with 
that A would be modified.

The tricky bits are:
- If A would no longer be accessed after B was modified, then that code 
will *currently* work.  Yet new optimizations could be added, that 
affect this (though really unlikely)
- could be a virtual method, and each version only accesses either A or B.
- it can not always be caught "Foo(s1,s2)" could have s1 and s2 have the 
same var (and with a ref count of 1, if both were "var param" to the 
caller).


More information about the fpc-devel mailing list