Skip to content

My Programming Language (Delphi) Can Do This

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!)".

{ 7 } Comments

  1. Kryvich | September 21, 2008 at 7:58 pm | Permalink

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

    No declaration of the anonymous method is needed.

  2. Kryvich | September 21, 2008 at 8:02 pm | Permalink

    Ooops, the brackets have disappeared.

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

  3. Jolyon Smith | September 22, 2008 at 11:34 am | Permalink

    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).

  4. Jolyon Smith | September 22, 2008 at 11:44 am | Permalink

    For completeness:

    type
    TCookingProc = procedure(aIngredient: String);

    procedure Potted(aIngredient: String);
    begin
    WriteLn(’Pot ‘ + aIngredient);
    end;

    procedure Boomed(aIngredient: String);
    begin
    WriteLn(’Boom ‘ + aIngredient);
    end;

    procedure Cook(aFirst, aSecond: String; aProc: TCookingProc);
    begin
    WriteLn(’Get the ‘ + aFirst);
    aProc(aFirst);
    aProc(aSecond);
    end;

    procedure DoCooking;
    begin
    Cook(’lobster’, ‘water’, Potted);
    Cook(’chicken’, ‘coconut’, Boomed);
    end;

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

  5. Stefaan Lesage | September 22, 2008 at 12:08 pm | Permalink

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

    Regards from Belgium,

    Stefaan

  6. Michael Madsen | September 23, 2008 at 3:38 pm | Permalink

    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.

  7. Joseph G. Mitzen | September 9, 2014 at 2:36 am | Permalink

    Honestly I think you’re all wrong. :-) First class functions were indeed a point of the article, as well as higher levels of abstraction. However, another key point was about applying the filter/map/reduce paradigm of functional programming to create the MapReduce algorithm. Filter/Map/Reduce operations still are nowhere near integrated as a paradigm into the heart of Delphi. Delphi added the bare minimal mechanics of closures and iteration but has never followed through with adding all the additional "gears and pieces" you need to use it as a paradigm such as, for instance, the object-relational paradigm.

    Delphi has always had first class functions. The problem is with its nested functions. Delphi didn’t NEED anonymous methods. All it needed to do was to be able to define the nested function anywhere in the function. However, because our nested "functions" are apparently some sort of hack, they can’t be used in a first class way. This was what necessitated anonymous methods, which are normally only for languages without first-class and nested functions. Anyonmous methods are rather un-Delphic (?) in syntax. Meanwhile, we could have used lambdas (short, one-line functions) but still haven’t gotten them.

    Python is one of the few other languages with nested functions. Guido Van Rossum indeed did resist all calls to add anonymous functions. He argued there was no difference between

    x = def (n):
    return n * n

    and

    def x(n):
    return n * n

    Wouldn’t it be much better (and easier to read) if the Delphi example above read:

    Procedure DoCooking;
    Begin

    Procedure Pot(s: String);
    Begin
    WriteLn(’pot ‘ + s);
    End;

    Cook(’lobster’, ‘water’, Pot)

    Procedure Boom(s: String);
    Begin
    WriteLn(’boom ‘ + s);
    End;

    Cook(’chicken’, ‘coconut’, Boom)

    End;

    Absolutely zero change in syntax needed! Unfortunately Delphi went for the more complex change instead and the awful syntax of entire procedures/functions written within a call to another function, complete with begins and ends… sigh.

Post a Comment

Your email is never published nor shared. Required fields are marked *

Bad Behavior has blocked 2 access attempts in the last 7 days.

Close