Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does [ref] do in a VCL application?

I'm working on a VCL application with Delphi 10 Seattle, and created a TDBGrid event handler via the IDE when I noticed that Delphi added a Ref custom attribute for the Rect argument:

procedure TfrmXxx.yyyDrawColumnCell(Sender: TObject;
  const [Ref] Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
begin
  //
end;
  • When or why does the IDE decide to insert this?
  • Does it have any effect in a VCL app?

update

Here's a video for those who cannot reproduce the behavior: enter image description here

like image 307
Wouter van Nifterick Avatar asked Apr 29 '16 14:04

Wouter van Nifterick


1 Answers

It is mentioned in the docs:

Constant parameters may be passed to the function by value or by reference, depending on the specific compiler used. To force the compiler to pass a constant parameter by reference, you can use the [Ref] decorator with the const keyword.

See Constant Parameters

When or why does the IDE decide to insert this?

The IDE never inserts this. It just copies the declaration of the event handler. Whoever wrote the event handler put the pass by[ref]erence marker in there.

Does it have any effect in a VCL app?

Yes.
If you mark a 8 byte parameter as const it will normally get passed by value in x64 and passed by reference in x86.
Declaring it as const [ref] will force it to be passed by reference in both cases.
It is very useful when doing inline assembly and in multi-threaded code.
Before const [ref] was introduced we were forced to use var instead of const to achieve the same effect.

like image 191
Uwe Raabe Avatar answered Nov 02 '22 19:11

Uwe Raabe