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?
相关问题
- Is there a Delphi 5 component that can handle .png
- Is there a way to install Delphi 2010 on Windows 2
- Is TWebBrowser dependant on IE version?
- iOS objective-c object: When to use release and wh
- DBGrid - How to set an individual background color
相关文章
- Best way to implement MVVM bindings (View <-> V
- Windows EventLog: How fast are operations with it?
- How to force Delphi compiler to display all hints
- Coloring cell background on firemonkey stringgrid
- Will java allow to use functional interfaces as me
- HelpInsight documentation in Delphi 2007
- Can RTTI interrogate types from project code at de
- How to scope closure's variables in CF10?
Anonymous methods are useful in functional programming, but they also can help you to write a more compact code in structured programming. Threading, for example: http://blogs.codegear.com/abauer/2008/09/08/38868
Another use cases for your 'excitement' :) : http://delphi.fosdal.com/2008/08/anonymous-methods-when-to-use-them.html
Just think of typical callback code where you need to have data available to the callback. Often this data is needed for the callback only, yet you have to jump through a number of hoops to get it there without having to resign to un-OOP-friendly practices like global variables. With anonymous methods the data can stay where it is - you don't have to unnecessarily extend its scope or copy it to some helper object. Just write your callback code in-place as an anonymous method and it can fully access and manipulate all local variables at the site where the anonymous method is defined (not where it's called!).
There are other aspects of anonymous methods, most obviously the fact that they are, well: anonymous, but this is the one that really made them go "click" for me...
I guess (I don't know Delphi) this implies that you can create functions as a kind of data object now. This means that you can, for example, pass functions as parameters to other functions. Example: A sort function might take a comparison function as a parameter, thus being much more versatile.
People have already provided the code, so I'll just list some places where they can be useful.
Say you have some GUI code. Normally, for something like a button's onclick handler, you have to provide a function that will be called when that button is clicked. However, let's say all that function has to do is something simple like pop up a message box or set a field somewhere. Let's say you have dozens of these buttons throughout your code. Without anonymous functions, you'll have to have tons of functions called "OnButton1Click," "OnExitButtonClick," etc, which will likely clutter up your code... or you can create anonymous functions that immediately attach to these events, and you don't have to worry about them anymore.
Another use is functional programming. Say you have a list of numbers. You want to get back only those numbers that are divisible by three. There is likely a function called
filter
which takes a function that returns a boolean and a list, and returns a new list containing only those elements in the first list that, when passed to the function, returned True. Example:It'd be annoying to be forced to define a function "isDivisibleByThree", then pass it to filter, so another use for anonymous functions here would be to just quickly create a function you won't need anywhere else and pass it to filter.
I'm answering my own question, but I found a good explanation of anonymous methods here Can your programming language do this?
May be this example can be of some value for you. Here I'm going to implement a zoomable display list for drawing on a TCanvas without declaring different types of display classes. It also makes heavy use of Generics. Assume we have a TForm with a TPaintBox and a TTrackBar on it...