Anonymous Methods

Top  Previous  Next

New features since Delphi 7 > Anonymous Methods

Delphi anonymous methods are declared using the keyword reference to. They represent closures: callable values that may capture variables from their surrounding scope.

Unlike method pointers, anonymous methods are not bound to an object instance and do not use the Code + Data method pointer representation.

 

Anonymous methods are commonly used for:

 

callbacks
predicates
inline logic passed to other routines
functions returned from functions

 

In the current automatic translation, anonymous methods are represented as callable C++ objects (lambdas or wrapper types).

 

Depending on the translation mode:

 

Portable mode → d2c::callback
C++Builder mode → SysUtils callable interfaces (TProc, TFunc__N, etc.)

 

1 Declaring Anonymous Method Types

 

Delphi

 

type

TProcRef = reference to procedure;

TFuncRef = reference to function(X: Integer): Integer;

 

C++ portable

 

typedef d2c::callback<void()>; TProcRef

typedef d2c::callback<int (int)>;TFuncRef

 

C++Builder

 

typedef  System::Sysutils::_di_TProc  TProcRef;

typedef  System::Sysutils::_di_TFunc__2<int,int> TFuncRef;

 

The generated type is a callable object that can store lambdas, free functions, or wrapped methods.

 

Assigning an Anonymous Method
Assigning an Existing Routine
Passing Anonymous Methods as Parameters
Returning Anonymous Methods from Functions
Anonymous Methods Used as Events

 

 

Anonymous Method Type Identity

 

Delphi

 

Distinct named reference to types are treated as different types, even if their signatures are identical.

 

C++

 

Distinct Delphi types are translated into distinct C++ typedefs or wrapper types, preserving type identity where possible and allowing overload resolution to match Delphi behavior.

 

Known Limitations related to anonymous methods

 

 

 



This page belongs to the Delphi2Cpp Documentation

Delphi2Cpp home  Content