Pascal gets closures before java - why hasn't the world ended?

Published: 11:07 PM GMT+12, Friday, 25 July 2008 under: technology
delphi  pascal  codegear 

So I see Delphi now has generics and closures in its upcoming "Tiburon" release:

type
  // method reference
  TProc = reference to procedure(x: Integer);               

procedure Call(const proc: TProc);
begin
  proc(42);
end;

Use:

var
  proc: TProc;
begin
  // anonymous method
  proc := procedure(a: Integer)
  begin
    Writeln(a);
  end;               

  Call(proc);
  readln
end.

I'd have loved have had both of these features back in the day when I was still doing Delphi. As much as I no longer like the Pascal syntax, I love how cleanly the anonymous method declaration sits within the natural flow of things (although I'm not sure an inline method directly on the call to Call() would sit well).

My only question is, if Pascal can evolve why can't java?

Sure the Tiburon release can do this because the underlying .NET VM now supports the functionality, but thats not the point. Ok?

Comments (10)

I agree with Shams, and to continue I personally don't think it belongs in an OO Language like Java. I am still finding it pretty hard to figure out what benefits a closure provides over a simple private method....

left by Arconan . Tuesday, 29 July 2008 11:08 PM

The new anonymous method in Delphi has no relation to the .NET since Tiburon will be a release that will entirely focus on Win32.... It seams this is the first step towards a LINQ style enabling in both Win32 & .NET Delphi ...

left by El Cy . Monday, 28 July 2008 7:02 PM

Disclosure: I'm an engineer working on the Delphi compiler, and I've been primarily responsible for the implementation of anonymous methods (as we're calling our version of closures).

To clarify: anonymous methods are supported for Win32 only, using reference counting for state management (since closures can contain captured state).

@Charmless: Our anonymous methods are indeed full closures, and they capture state - it's just that this example doesn't show that.

left by Barry Kelly . Monday, 28 July 2008 4:43 PM

Delphi has had inner functions that can refer to their enclosing lexical scope since at least 5.0. Unfortunately, you couldn't take the address of them.

left by Chris . Monday, 28 July 2008 12:26 PM

It will be even better than you think. Anonymous method and generics will be added also to native Delphi (not Net version). So it is done without net vm help.

left by Filip . Monday, 28 July 2008 10:59 AM

That example doesn't display use of closures. No state was carried by the method object - it didn't "close around" any values.

left by Charmless . Monday, 28 July 2008 10:34 AM

As far as I know closures in JAVA are now under discussion. And for know the inner class are used as some sort of closures.

left by Radoslav Stankov . Monday, 28 July 2008 8:43 AM

I'm skeptical about how useful closures will be after they are introduced in java. I'm afraid people will get carried away and misuse the feature, I have a similar feeling about java reflection.

P.S. What's with the red font color in the comment form?

left by Shams Mahmood . Monday, 28 July 2008 3:58 AM

Closure is a cool feature. It has made a way to PHP 5.3 already.

left by pcdinh . Saturday, 26 July 2008 7:44 PM

I'm sure Java will get some form of closure/anonymous function sometime. Whether it will be ghastly is a large possibility, though.

PS: This commenting system is way too complex!

left by Chris Broadfoot . Saturday, 26 July 2008 12:17 AM
Add Comment