Hello!

Hello!

I was reading this article earlier:
http://encosia.com/first-class-functions-as-an-alternative-to-javascripts-switch-statement/

And I think it could be doable in Delphi as well by using Generics.
Alas, I am only using them in a very basic way, therefore I need some help to craft an example in Delphi.

Anybody can please give me some tips on how to do it?

Regards,

A
http://encosia.com/first-class-functions-as-an-alternative-to-javascripts-switch-statement/

Comments

  1. I did something similar i C++ with Boost.Bind for handling a simple RPC-style protocol. In Delphi you might run into some issues with generics being too generic... Would like to see if someone comes up with a clever solution though :)

    ReplyDelete
  2. Here's my "port" of the JS example: http://nopaste.dk/p45110

    Think it ended up pretty close, and not too hampered by generics not being templates (boy do I miss templates).

    ReplyDelete
  3. Looks pretty neat - I think I have to get into these topics more :)

    I suspect something like that to be far more flexyble than a case statement.

    Regards(and thanks),

    A

    ReplyDelete
  4. Oh yes definitely more flexible. Of course you could change where and how the "handlers" are added, perhaps a singleton is appropriate.

    ReplyDelete
  5. I am not a fan of singletons, to be honest... I can see the cases where such constructs are not inappropriate, but there are plenty of cases where they are really abused...

    ReplyDelete
  6. True, one should be very careful with singletons. Often a "context singleton" makes sense though. Again, depends on the design. In my RPC case, the function map and handlers are all contained in the implementation section, so not visible outside.

    ReplyDelete
  7. For my singletons, IInterface in the interface section, everything else in
    the implementation one.

    Regards,

    A

    ReplyDelete

Post a Comment