Recent

Author Topic: Use of @  (Read 559 times)

Ever

  • New Member
  • *
  • Posts: 32
Use of @
« on: October 24, 2019, 04:12:01 am »
Regards

    I am still learning to program in pascal, so I have a doubt as to the syntax of the language for which I need please explain its operation.

     In some examples that I analyze with the intention of instructing myself, I see that they make calls to procedures and put the @ symbol before it, I give an example:

Code: Pascal  [Select]
  1. procedure TForm1.RichMemo1PrintAction(Sender: TObject;
  2.   APrintAction: TPrintAction; PrintCanvas: TCanvas; CurrentPage: Integer;
  3.   var AbortPrint: Boolean);
  4. begin
  5.   if APrintAction=paPageStart then begin
  6.     PrintCanvas.Brush.Color:=clBlue;
  7.     PrintCanvas.Brush.Style:=bsSolid;
  8.     PrintCanvas.Ellipse(100,100,200,200);
  9.   end;
  10.   writeln('action: ', APrintAction);
  11. end;  
  12.  
  13. procedure TForm1.FormCreate(Sender: TObject);
  14. begin
  15.   RichMemo1.OnPrintAction:=@RichMemo1PrintAction;
  16. end;    
  17.  

 By calling the RichMemo1PrintAction procedure, you put the @ before, why is that done? I can't understand it at all. I think it is because the property to which reference is made and to which some value must be stored, is assigned in this way the result of the procedure that is called ?, I do not know if that is why.

   Please, thank you who can help me understand my doubt

Sincerely,

Ever Delgado

lainz

  • Hero Member
  • *****
  • Posts: 3284
    • Lainz
Re: Use of @
« Reply #1 on: October 24, 2019, 05:08:55 am »
Hi Ever,

When using @function or @procedure or @procefure of object or @function of object, it means it's assigning the function to a variable allowing to be called later.

For example

Button1.OnClick:=@onclickbutton

When you click the button the function onclickbutton is called.

Inside the button is something like

If assigned(onclick) then onclick (self)

Onclick is replaced with the function you assigned before.

Thaddy

  • Hero Member
  • *****
  • Posts: 9190
Re: Use of @
« Reply #2 on: October 24, 2019, 06:43:13 am »
You forgot to mention this assumes {$mode objfpc} for @ and method pointers. In {$mode delphi} it is not necessary, see https://www.freepascal.org/docs-html/user/usersu88.html#x132-1390007.3.3
also related to equus asinus.

MarkMLl

  • Full Member
  • ***
  • Posts: 242
Re: Use of @
« Reply #3 on: October 24, 2019, 02:55:20 pm »
By calling the RichMemo1PrintAction procedure, you put the @ before, why is that done?

Short answer: you'd find it clearer if RichMemo1PrintAction were a function rather than a procedure, it's to make sure that you're using a pointer to the function rather than calling the function and using its result.

Everybody else is expanding on that in more detail.

MarkMLl

Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

fmc

  • New Member
  • *
  • Posts: 27
Re: Use of @
« Reply #4 on: October 25, 2019, 12:12:56 am »
Hi Ever.

I found this site not long ago. Section 7.8  addresses what you're asking. The whole thing's a good read. Check it out.
Win X Pro / Lazarus 2.0.6 / FPC 3.0.4

Ever

  • New Member
  • *
  • Posts: 32
Re: Use of @
« Reply #5 on: October 25, 2019, 04:16:21 am »
Thank you all for your explanations, I will review the links given to further understand the operation and practice.

Grateful  :D

Thaddy

  • Hero Member
  • *****
  • Posts: 9190
Re: Use of @
« Reply #6 on: October 25, 2019, 09:56:13 am »
By calling the RichMemo1PrintAction procedure, you put the @ before, why is that done?

Short answer: you'd find it clearer if RichMemo1PrintAction were a function rather than a procedure, it's to make sure that you're using a pointer to the function rather than calling the function and using its result.
MarkMLl

Well, that is very clear: it is a method, in this case a procedure of object, See his code:
Code: Pascal  [Select]
  1. procedure TForm1.RichMemo1PrintAction(Sender: TObject;
  2.   APrintAction: TPrintAction; PrintCanvas: TCanvas; CurrentPage: Integer;
  3.   var AbortPrint: Boolean);
« Last Edit: October 25, 2019, 09:58:50 am by Thaddy »
also related to equus asinus.

MarkMLl

  • Full Member
  • ***
  • Posts: 242
Re: Use of @
« Reply #7 on: October 25, 2019, 10:27:17 am »
Well, that is very clear: it is a method, in this case a procedure of object, See his code:

I wasn't saying that his example was unclear. I was saying that the rationale for inserting the @ would be clearer if considering a function.
« Last Edit: October 25, 2019, 12:32:58 pm by MarkMLl »
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

Mr.Madguy

  • Sr. Member
  • ****
  • Posts: 457
Re: Use of @
« Reply #8 on: October 25, 2019, 12:30:44 pm »
As always. For disambiguation purposes. To distinguish "procedure variable = procedure" assigning from "some variable = result of function call" assigning. Because some function can have "function A:TFunction" declaration. Delphi does it implicitly. In all cases, when
1) Destination is procedure/function type
2) It's procedure or function result isn't procedure/function type
3) There are no () after procedure procedure/function name
Delphi assumes @ symbol before procedure/function name.
DynamicData 3.0 is released!
Since now development is frozen - only optimization passes will be made at some point.
Lack of multiple inheritance turns it into abomination.