Pascal gets closures before java - why hasn't the world ended?
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)
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 ...
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.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.
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.
That example doesn't display use of closures. No state was carried by the method object - it didn't "close around" any values.
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.
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?Closure is a cool feature. It has made a way to PHP 5.3 already.
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!
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....