Archive for the ‘Anonymous Methods’ Category.

Delayed action

When you work with UIs, order of operations can be very important.  Windows’s UI works based on an event queue and a message pump that reads events from the queue and dispatches them.  And since the UI is single-threaded, this means that at any time, there could be pending events about to execute while you’re handling some UI code.

Sometimes you’ll get into a situation where you need to execute something, but not right away; you need it to happen after all of the pending events have processed.  Now, obviously, the best way to make something happen after everything in the queue has processed is to put something else onto the queue directly behind everything that’s currently in there.  But the tricky part is what happens when it comes back out. Continue reading ‘Delayed action’ »

Beware using anonymous methods in loops

Quick, what’s the output of this simple routine?
Continue reading ‘Beware using anonymous methods in loops’ »

How to leak a class you never defined

Quick, what’s wrong with this code?

[code lang="delphi"]
procedure ContrivedExample;
var
   factorial: TFunc;
begin
   factorial :=
      function(input: Integer): integer
      begin
         if (input = 0) or (input = 1) then
            result := input
         else result := input * (factorial(input - 1));
      end;

   writeln(factorial(5));
end;
[/code]

Continue reading ‘How to leak a class you never defined’ »

Under the hood of an anonymous method

I woke up this morning and checked DelphiFeeds, and found a very interesting post by Jolyon Smith about the use of the absolute keyword.  That reminded me that I had to go and write up this article.  Why?  Because it’s the only way I know of to get inside an anonymous method’s functor object and do some looking around.

Continue reading ‘Under the hood of an anonymous method’ »

What’s in a name-less method?

When I first saw the announcements for the new Delphi 2009 features, a little more than a year ago, my reactions went something like this:

Unicode: Hmm… looks interesting.
Generics: YES! FINALLY!
Anonymous methods: …huh?

I think that’s pretty much how everyone reacted to anonymous methods at first.  Continue reading ‘What’s in a name-less method?’ »