Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone explain Anonymous methods to me?

Delphi 2009, among some cool stuff, has also just got Anonymous methods. I've seen the examples, and the blog posts regarding anonymous methods, but I don't get them yet. Can someone explain why I should be excited?

like image 858
Steve Avatar asked Nov 01 '08 21:11

Steve


People also ask

What is true about anonymous method?

Anonymous methods are self-explanatory, implying "No Name" (anonymous) methods that let you declare a method body without giving it a name. Simply an anonymous method has only a body without a name, optional parameters and a return type. Anonymous methods can only be created when using delegates.

What are anonymous methods?

Anonymous method is a block of code, which is used as a parameter for the delegate. An anonymous method can be used anywhere. A delegate is used and is defined in line, without a method name with the optional parameters and a method body. The scope of the parameters of an anonymous method is the anonymous-method-block.

What is the advantage of using anonymous methods?

The advantage of an anonymous function is that it does not have to be stored in a separate file. This can greatly simplify programs, as often calculations are very simple and the use of anonymous functions reduces the number of code files necessary for a program.

What is a negative aspect of anonymous methods?

Another disadvantage of anonymous functions is that a new function is created every time the code that uses it runs, which in some cases makes no difference at all, but in other cases may be something to consider for performance reasons (e.g., if doing things in a loop)


1 Answers

Please have a look at closures.

Delphi anonymous functions are closures.

These are created within other functions and as such has access to the scope of that function. This is even so if the anonumous function is assigned to a function parameter that is called after the original function is called. (I will create an example in a moment).

type   TAnonFunc = reference to procedure;   TForm2 = class(TForm)     Memo1: TMemo;     Button1: TButton;     Button2: TButton;     Button3: TButton;     procedure Button1Click(Sender: TObject);     procedure Button2Click(Sender: TObject);     procedure Button3Click(Sender: TObject);   private     F1 : TAnonFunc;     F2 : TAnonFunc;   end;  procedure TForm2.Button1Click(Sender: TObject); var   a : Integer; begin   a := 1;    F1 := procedure   begin     a := a + 1;   end;    F2 := procedure   begin     Memo1.Lines.Add(IntToStr(a));   end; end; 

The above method assigns two anonymous functions to the fields F1 and F2. The first increases the local variable and the second showe the value of the variable.

procedure TForm2.Button2Click(Sender: TObject); begin   F1; end;  procedure TForm2.Button3Click(Sender: TObject); begin   F2; end; 

You can now call both functions, and they access the same a. So calling F1 twice and F2 once shows a 3. Of course this is a simple example. But it can be expanded to more usefull code.

In the multi threading environment, anonymous functions can be used in a call to Synchronise, which eliminates the need for countless methods.

like image 61
Toon Krijthe Avatar answered Oct 24 '22 06:10

Toon Krijthe