Defer defines the “postpone procedure” pattern, this postpone should schedule a “procedure: TProc” to run it after the end of the caller method that executed the call to the Defer function (Proc: TProc).


Defer defines the “postpone procedure” pattern, this postpone should schedule a “procedure: TProc” to run it after the end of the caller method that executed the call to the Defer function (Proc: TProc).

At the end of the caller method, Defer must execute all stacked procedures in the reverse order in which they were scheduled.


http://blog.cesarromero.com.br/en/posts/delphi-defer/

Comments

  1. Would be cool if you could merge this into Spring4D.

    ReplyDelete
  2. Stefan Glienke although in most cases you can achieve the same goal, the details is what give the best of each pattern or feature.
    I have been using SmartPointer for a long time to manage object lifetime, Defer postpone a method call, I can Defer any "TProc" method directly, and that is where I have most used so far, Closing files, Unlocking mutexes, managing database transactions, "EndUpdates" methods, even Object.Free, with that I could remove some SmartPointers I have used just to manage object lifetime.

    If you try to use SmartPointer to postpone method call, will it not have boillerplate?

    Some examples in the article is showing how to do it with anonymous methods, that is convenient, just a plus, even with all the verbose delphi way it can be usefull.

    Last weekend I have done another implementation without the internal stack, just using a TProc field, transfering the deallocating order to the Memory Manager, so far it is working just fine, I'll test for more couple days before push it.

    Both Defer and SmartPointers are usefull for me, now I have the option to choose where to use each.

    ReplyDelete
  3. I was referring to the RAII pattern which is nothing new. The different applications might. Also FWIW it has nothing to do with the memory manager but how the compiler clears the implicit interface results in the epilogue of the routine that it made space for in the prologue.

    ReplyDelete
  4. Ic, that RAII Pattern is used for most new things in Delphi lately, ie. anonymous methods implementation

    ReplyDelete
  5. I remember why I like it: wiert.me - Delphi: a memento that executes any code at end of method but your construct has unit tests and a better interface.

    Now go translate the docs into English (;

    ReplyDelete

Post a Comment