Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi> Please explain this: type... procedure of object

Tags:

delphi

I've encountered some code that's new to me...

I've never really seen a type declaration of a procedure of object, and I just don't see the point. Why couldn't the developer simply keep a field of type Boolean?

interface

    type
      TFinishedCaptureEvent = procedure(AFinished: Boolean) of object;

      TFrameCard = class(TFrame)
      ...
      private
        FOnFinishedCapture: TFinishedCaptureEvent;
        procedure DoUpdateMessage(AMessageType: TMessageType);
      public
        property OnFinishedCapture: TFinishedCaptureEvent read FOnFinishedCapture write FOnFinishedCapture;
      end;    

implementation

...

procedure TFrameCard.DoUpdateMessage(AMessageType: TMessageType);
begin
  if Assigned(FOnFinishedCapture) then
    FOnFinishedCapture(False);
  ...
end;

end.
like image 930
Richard Woolf Avatar asked Jan 07 '11 14:01

Richard Woolf


People also ask

What is a procedure in Delphi?

A procedure is a routine that does not return a value. Function calls, because they return a value, can be used as expressions in assignments and operations. For example: I := SomeFunction(X); calls SomeFunction and assigns the result to I.

What are objects in Delphi?

Objects are dynamically allocated blocks of memory whose structure is determined by their class type. Each object has a unique copy of every field defined in the class, but all instances of a class share the same methods. Objects are created and destroyed by special methods called constructors and destructors.

What is type procedural?

Procedural types allow you to treat procedures and functions as values that can be assigned to variables or passed to other procedures and functions. This topic does not refer to the newer type of procedural type used with anonymous methods, that is, a "reference to a procedure".


2 Answers

Let's break this down into two parts to be easier to understand. First, procedure(AFinished: Boolean) isn't a boolean variable, it's a reference to a procedure that takes a boolean as a parameter. It's basically a procedure header, except without the procedure name because this is just a type definition. Any procedure that matches this signature can be assigned to this variable.

The of object part means that this isn't just a procedure reference, but a method reference; it has to belong to an object. The compiler needs to know the difference so that it can store the self pointer for the object together with the procedure pointer so it can be invoked properly, as the other posters have pointed out.

Basically, this is declaring a new event handler, and it's a pretty common pattern in Delphi. It's the same thing that the VCL does all over the place. When you create a button and assign an OnClick handler, it has to be a procedure (Sender: TObject) of object;. Your form gives the button a method reference referring to itself and the event handler procedure, and then the button uses that information to invoke the handler on the form when someone clicks it.

This code is doing the same thing. It's providing a way for some external object to be notified when DoUpdateMessage runs, using the standard Delphi idiom for event notification.

like image 189
Mason Wheeler Avatar answered Oct 29 '22 10:10

Mason Wheeler


I don't understand how you relate this to a field of boolean.

But TFinishedCaptureEvent = procedure(AFinished: Boolean) of object declares a delegate/method pointer type, which is used for events. It's a record which contains a self pointer and a function pointer. When you call the delegate, the function is called with the self passed as a parameter to the function.

like image 45
CodesInChaos Avatar answered Oct 29 '22 08:10

CodesInChaos