Yeah. Closures are actually the best way to do it:
Actually, the closure adds little to your example; it's the
reference declaration that does everything there (and allows the unnamed function to work), but AFAIK Free Pascal hasn't got that kind of procedural type declaration.
As for Thaddy's suggestion, it's good to know the thing about static class methods but it doesn't really helps in this case. The point was to simplify the suggested solution; yours, though, would at the very least entail subclassing
TFileSearcher and making sundry modifications that would end up complicating matters.
The point was (as an example) building a substitute
FindAllFiles() with a procedural parameter to be called for each file found. The original
FindAllFiles() uses a
TFileSearcher, so passing a method to it is as simple as setting a property with the parameter. The complication comes because we want to be able to use also a normal procedure.
The easy solution is to use an intermediate event handler in the
implementation which will be used to call the "normal" procedure when we use that variant, but then we would need an "implementation" global. I was hoping to find a more simple way to do it, without subclassing TFileSearcher or complicating matters much more.
But it seems what I already found (save for some details) is the most simple.
