Home > D Programming Language, programming > Should D have real closures?

Should D have real closures?

September 13th, 2007

It has been a vivid debate following my D doesn’t have real closures post. For most parts it has been a constructive discussion, but what I wanted to see was real examples that would convince me that real closures (those that carry the environment with them) were actually useful for a language that provides other means of object oriented programming.

One reason for my skepticism was an interview with Ruby’s creator Yukihiro Matsumoto, made by Bill Venners. There Mr Matsumoto says this about having the context captured: “it’s not that useful in the daily lives of programmers”. Here is the statement in context:

Bill Venners: OK, but what is the benefit of having the context? The distinction that makes Ruby’s closure a real closure is that it captures the context, the local variables and so on. What benefit do I get from having the context in addition to the code that I don’t get by just being able to pass a chunk of code around as an object?

Yukihiro Matsumoto: Actually, to tell the truth, the first reason is to respect the history of Lisp. Lisp provided real closures, and I wanted to follow that.

Bill Venners: One difference I can see is that data is actually shared between the closure objects and the method. I imagine I could always pass any needed context data into a regular, non-closure, block as parameters, but then the block would just have a copy of the context, not the real thing. It’s not sharing the context. Sharing is what’s going on in a closure that’s different from a plain old function object.

Yukihiro Matsumoto: Yes, and that sharing allows you to do some interesting code demos, but I think it’s not that useful in the daily lives of programmers. It doesn’t matter that much. The plain copy, like it’s done in Java’s inner classes for example, works in most cases. But in Ruby closures, I wanted to respect the Lisp culture.

Today I read a blog post from Julio César who’s arguing for incorporating real closures in D. He’s providing real examples and his case feels strong. Also, he points to an alternative way of implementing it that doesn’t look too complicated.

Be Sociable, Share!
Categories: D Programming Language, programming Tags:
Comments are closed.