When to use and not use @?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

When to use and not use @?

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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: When to use and not use @?

Michael Van Canneyt


On Sun, 10 Sep 2017, 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.

>
> 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.

>
> 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
   P:=@MyProcedure;
end.


> Is there an authoritative document describing this for FPC somewhere?


How about the Language reference guide ?

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

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal