My Programming Language (Delphi) Can Do This

Posted by on in Blogs
joelonsoftware asks on his blog "Can Your Programming Language Do This?". Delphi 2009 introduces support for anonymous methods in Delphi language. Yes, Delphi can do this:-)

For your convinience here is the relevant snippet from Joel's blog:


Delphi, contrary to JavaScript, is a strongly-typed language. This is the reason you need to define an anonymous method signature.  I think it is a good thing.

Here is the output from a simple console application:

I have my private theory about Joel's lastname which is "Spolsky". I'm Polish myself and I guees there is some Polish connection here. "Spolsky" sounds like "z Polski" in Polish language, which is "from Poland" but just made more English-looking like.

And make sure you visit "Stack Overflow" for ultimate programmers' FAQ system.
Creating an account on Stack Overflow is tricky. I had to create a blog account on blogspot server to obtain an openID.

Delphi 2009 and C++Builder 2009 introduce full Unicode support. Here is "The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)".

Gold User, Rank: 9, Points: 364
Crazy about Delphi Programming!


  • Guest
    Kryvich Sunday, 21 September 2008

    Apparently you can write it as
    procedure Cook(s1, s2: string; p: TProc);

    No declaration of the anonymous method is needed.

  • Guest
    Kryvich Sunday, 21 September 2008

    Ooops, the brackets have disappeared.

    Should be
    procedure Cook(s1, s2: string; p: TProc LEFT_ANGLE String RIGHT_ANGLE );

  • Guest
    Jolyon Smith Monday, 22 September 2008

    Indeed, I think Pawel somewhat missed the point of Joel's article.

    When Joel asks "Can your language do this?" he wasn't asking "Does your language have anonymous methods?". He was referring to using functions as things that can be passed around, e.g. to be invoked later/elsewhere.

    Some languages cannot do this and *need* anonymous methods.

    Delphi didn't have anonymous methods before Delphi 2009, but has always had "first class functions". Users of Delphi 1.0 would have answered "Yes" to Joel's question.

    Anonymous methods do of course add more, in the form of more flexible and, some would say, powerful closures, but these are potentially also far more confusing (for the poor old human reader, not the compiler).

  • Guest
    Jolyon Smith Monday, 22 September 2008

    For completeness:

    TCookingProc = procedure(aIngredient: String);

    procedure Potted(aIngredient: String);
    WriteLn('Pot ' + aIngredient);

    procedure Boomed(aIngredient: String);
    WriteLn('Boom ' + aIngredient);

    procedure Cook(aFirst, aSecond: String; aProc: TCookingProc);
    WriteLn('Get the ' + aFirst);

    procedure DoCooking;
    Cook('lobster', 'water', Potted);
    Cook('chicken', 'coconut', Boomed);

    (not tested - not even compiled. These comment editors don't make very good IDE's - lol)

  • Guest
    Stefaan Lesage Monday, 22 September 2008

    Nice piece of code. I didn't even know that was possible :-)

    Regards from Belgium,


  • Guest
    Michael Madsen Tuesday, 23 September 2008

    Actually, Joel does mention that he wants to be able to declare the function in-line (there's a comment about function pointers in C, where he mentions the whole "having to declare the function elsewhere" as being a bit of a burden) - so he *does* actually ask for anonymous methods.

  • Please login first in order for you to submit comments
  • Page :
  • 1

Check out more tips and tricks in this development video: