Sign in Join

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

Mailing lists / fpc-pascal / 2017-09

When to use and not use @?

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Bo Berglund

Coming from Delphi I am a bit confused about the syntax when dealing with procedure arguments specified as var...

In Delphi this is all taken care of by the compiler and the declaration of the procedure tells it how to deal with the arguments. Like so:

procedure DoSomething(var Cnt: integer);

called in FPC by:

DoSomething(@MyCount);

But in Delphi it is just:

DoSomething(MyCount);

OK, I can live with this, but next comes objects and dynamic arrays and such...

When is it appropriate to use @ and when should it not be used?

procedure SendData(var Buf: TBytes);
procedure DigOutStuff(var MyCase: TCaseObject);

Are these also called with @ or something else?
I think that in Delphi it is enough to use the variable name in all cases.

Is there an authoritative document describing this for FPC somewhere?

My current case is sending data from a thread to an event in the main GUI app where the data are located in a TBytes object and I don't want to copy the data unneccesarily...
I would like to supply the thread buffer to the event so that the event procedure can copy the data into a local array and process that.

But if I declare the event procedure without using var I am afraid that just using the buffer as an argument will make the compiler *copy* the data into the call and then there will be two copy operations...

Bo Berglund Developer in Sweden

Reply by email

Michael Van Canneyt
to Bo Berglund
Bo Berglund wrote
Coming from Delphi I am a bit confused about the syntax when dealing with procedure arguments specified as var...

FPC is exactly the same regarding this.

Show quoted text 10 lines
In Delphi this is all taken care of by the compiler and the declaration of the procedure tells it how to deal with the arguments. Like so: procedure DoSomething(var Cnt: integer); called in FPC by: DoSomething(@MyCount);

No it is not.

But in Delphi it is just: DoSomething(MyCount);

It is the same in FPC.

Show quoted text 10 lines
OK, I can live with this, but next comes objects and dynamic arrays and such... When is it appropriate to use @ and when should it not be used? procedure SendData(var Buf: TBytes); procedure DigOutStuff(var MyCase: TCaseObject); Are these also called with @ or something else?

No, why do you think so ?

I think that in Delphi it is enough to use the variable name in all cases.

It is exacly the same in FPC.

The only place where FPC differs from Delphi is when you assign a procedure to a procedural variable.

Procedure MyProcedure;
begin
end;
Var
   p : Procedure;
begin
   ;
end.
Is there an authoritative document describing this for FPC somewhere?

How about the Language reference guide ?

https://www.freepascal.org/docs.var

Michael.

Reply by email