Delphi delegate types are interfaces

I knew anonymous functions were implemented as interfaces.

This, I hadn’t thought about.

Thanks to @sglienke for 2011.
I’m only 13 years behind. :smiling_face_with_tear:


Quoting ::

Well, a few versions ago we got method references. You prolly know how they are implemented behind the scenes but let’s take a quick look at them. You can do something like (as defined in SysUtils):

type
  TFunc<T> = reference to function: T;

As you also may know this is a reference counted type. Guess how it is implemented? Correct, with an interface. This is similar to:

type
  IFunc<T> = interface
    function Invoke(): T;
  end;

You can even use those method reference types as Interface when you implement a class:

type
  TMyFuncClass = class(TInterfacedObject, TFunc<Integer>)
  public
    function Invoke: Integer;
  end;

Or you can inherit your interface from it (and of course implement that interface into your class):

type
  IFunc<T> = interface(TFunc<T>)
  end;