Anonymous Method Usage
So now that Beta 1 has been out and fairly widely availible for a while, how do you use anonymous methods?
When I was working on them I saw several contrived examples. I saw several that seemed more like abuses than legitimate uses. In my earlier posts I pointed out some of the problems the compiler has with 'optimizing' the code generated for anonymous methods and the methods that contain them. In many cases I simply had to brainstorm with other until we came up with a logical guess for the most likely use and a solution that worked well for that case while not overly penalizing the less common cases.
Are you a functional programmer that likes doing completions? Do you try and morph C# into something more like Scheme or Lisp? (I've seen code like this, but I've never felt comfortable writing code like this)
Are you more of a Pascal hacker that uses anonymous methods like nested methods? (I've done this a few times)
I haven't found a good name for this last style, so for now I'll call it the lazy style: where you use an anonymous method instead of real method just because it's faster. Like when a WinForms event handler only has one line of code, it just seems silly to write a whole method for that one line. I have also seen this taken to extremes: a WinForm with all the event handlers inlined as anonymous methods into the form's InitializeComponents method! In my mind this makes sense for small methods, but not big ones.
Lastly, how you found problems with anonymous methods that prevent your from using them where you want to? For example: the performance is too slow, too much memory overhead, etc.
--Grant
Comments
- Anonymous
August 10, 2004
In my expirience anonymous methods (or lambda expressions http://nemerle.org/grokking.html#lambda-expr like we used to call them) are used mostly in collections processing (iterating, maping a function to every element or folding). This is useful when you are programming in functional style.
Second application is just like you said - for handlers of various events. You don't need to define private functions floating around to do simple one or two like tasks.
But anonymous functions, delegates, events are just a hackish way of approaching a general methodology of functional values ( http://nemerle.org/grokking.html#functional-values ). Implementing anonymous methods and delegates you have to create closures, so there is only one little step to unify all those concepts. - Anonymous
August 10, 2004
Polymorphic conditionals
Like so:
public delegate void NiladicMethod();
public class State
{
public virtual void IfIdle(NiladicMethod method)
{
}
}
public class IdleState
{
public virtual void IfIdle(NiladicMethod method)
{ method(); }
}
in my code:
void OnStateChanged(State state)
{
//some people might do something naughty like:
if (state is IdleState) {
}
// cleverer people might declare an IsIdle() method:
if (state.IsIdle()) {
}
// i prefer anonymous method:
state.IfIdle(delegate() {
// we're idle
});
} - Anonymous
August 10, 2004
sorry, IdleState should extend State, and override IfIdle - Anonymous
August 10, 2004
Grant,
I'd refer to the third style as pseudo-inlining.