1. Technology

Understanding Anonymous Methods in Delphi

Learn How To Write Less Code For More Power

By

With the inclusion of anonymous methods in Delphi 2009, Delphi language has a new weapon in its "what a routine can do and be used for, and how" arsenal.

In short, an anonymous method is a procedure or function that does not have a name associated with it.

Anonymous methods extend existing procedural types and by allowing you use the actual code of a method as a parameter to some other method. In addition, an anonymous method can refer to variables and bind values to the variables in the context in which the method is defined.

Delphi anonymous methods are similar to the construct of closures defined in other languages.

Here's a list of articles explaining anonymous methods in Delphi with usage examples:

What and why and how on Anonymous Methods in Delphi

The Many Faces of Delphi Routines: Functions and Procedures
Here's an overview of how and when and for what you can use various "types" of Delphi routines: functions and procedures, function and procedure pointers, methods and method pointers, anonymous methods...

Using New Delphi Coding Styles and Architectures
Anonymous methods are a brand new feature for Delphi, but they've been around in different forms and with different names for many years in other programming languages--most notably dynamic languages.

Sort a Generic List using Anonymous Comparer Method
Using anonymous methods you can apply the sorting function directly "inline" with the call to the Sort method.

Fun with generics and anonymous methods
A "method reference" can refer to any global function, method or anonymous method which takes no parameters and returns a value...

Anonymous Methods and Closures in Delphi
To describe them in one sentence is unfair, but I'll try. They are methods which are defined inline (you might call them literal methods) and hence are not called by name.

Dr.Bob Examines...Anonymous Methods
Anonymous Exit; Comparing with anonymous methods....

Using anonymous methods in method pointers
Anonymous methods may have associated state. In particular, all variables that an anonymous method captures need to be kept alive so long as the anonymous method is callable.

An Anonymous Example

Therefore, an anonymous method type is declared as (example of an anonymous procedure taking an integer as a parameter):
type TIntProc = reference to procedure (const cn: integer);
If you finished reading on anonymous methods, it should not be a problem to figure out what the following code does :)
Sort(TComparer.Construct(
  function (const a, b: TMyCodeType): integer
  begin
    result := CompareText(a.Code, b.Code);
  end
));
The above code uses an anonymous method to sort a TList.

Here's another example where an anonymous method is used in TDirectory's GetFiles function to grab all "*.pas" files under "c:\":

TDirectory.GetFiles(
  'c:\',
  TSearchOption.soTopDirectoryOnly,
  function(const Path: string; const SearchRec: TSearchRec): Boolean
  begin
    result := ExtractFileExt(SearchRec.Name) = '.pas';
  end
);
Along with generics, anonymous methods were the reason for me to move (from Delphi 7 / 2007) to Delphi 2009 (and newer).

©2014 About.com. All rights reserved.