Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2005-06

Passing consts to const

7 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

David Butler

Hi

The following code snippet gives a compile error: "Error: Can't assign values to const variable".

It should be legal, since we are passing a const to a const parameter.

It should only give that error if TestBuf was declared as "var Buf" instead of "const Buf".

Regards
David

{$WRITEABLECONST OFF}
procedure TestBuf(const Buf; Size: Integer);
begin
end;
const A : Byte = 1;
begin
  TestBuf(A, 1);
end.

Reply by email

Kornel Kisielewicz
to David Butler
David Butler wrote 7 lines
{$WRITEABLECONST OFF} procedure TestBuf(const Buf; Size: Integer); begin end; const A : Byte = 1;

From this point A is treated as a normal variable with an initial assignment...

begin TestBuf(A, 1);

... so it is not const here.

Guess if you would substitute the line

const A : Byte = 1;

with

const A = 1;

It should work. AFAIR, typed constants are treated exactly as variables with an initial value.

At your service, Kornel Kisielewicz (charonATmagma-net.pl) [http://chaos.magma-net.pl] Carceri -- A prelude to GenRogue... Coming Soon

Reply by email

Alexey Barkovoy
to Kornel Kisielewicz
Show quoted text 7 lines
procedure TestBuf(const Buf; Size: Integer); begin end; It should work. AFAIR, typed constants are treated exactly as variables with an initial value.

Actually untyped parameters accept anything except numeral or untyped numeric constant in Delphi. The meaning of const in this example is contract that function will not change passed value.

Reply by email

David Butler
to Kornel Kisielewicz: From this point A is treated as a normal variable with an initial assignment... ... so it…
you wrote 17 lines
David Butler wrote: > {$WRITEABLECONST OFF} > > procedure TestBuf(const Buf; Size: Integer); > begin > end; > > const A : Byte = 1; From this point A is treated as a normal variable with an initial assignment... > > begin > TestBuf(A, 1); ... so it is not const here.

It is perfectly legal to pass a "var" to a "const". The problem is that FPC thinks we are trying to change A.

Show quoted text 8 lines
Guess if you would substitute the line > const A : Byte = 1; with > const A = 1; It should work. AFAIR, typed constants are treated exactly as variables with an initial value.

Exactly. If it is treated like a normal variable, why the error message?

The problem is not with the "const A" declaration, the problem is with the the call to TestBuf not allowing a "constant variable" to be passed (like Delphi rightly does).

Although the TestBuf call compiles with your suggested "const A = 1" declaration in FPC, it doesn't make sense (and it rightly doesn't compile in Delphi).

I don't want to say FPC should be doing everything the way Delphi does, but in this case it makes sense, in my opinion.

Regards
David

Reply by email

Peter Vreman
to David Butler
I don't want to say FPC should be doing everything the way Delphi does, but in this case it makes sense, in my opinion.

Please report bugs/compatibilities to the bugs repository. Bugs posted on the mailing list tend to be forgotten.

Reply by email

David Butler
to Peter Vreman
you wrote 5 lines
> I don't want to say FPC should be doing everything the way Delphi > does, but in this case it makes sense, in my opinion. Please report bugs/compatibilities to the bugs repository. Bugs posted on the mailing list tend to be forgotten.

Added. Thanks Peter.

Reply by email