5

Consider a method m of a class A in an object-oriented language like Java or C++.

In the body of the method m it is possible to reference the member variables of the object on which the method is invoked. These variables are not defined in the method itself but are bound in the moment in which the method is invoked.

Is this the same mechanism used for closures (binding non-local names using the context) or, if not, what are the differences?

In other words, would it be possible to view an object with its methods as a collection of closures (the methods) that are closed over some common data (the object's member variables)?

4
  • 2
    An encapsulation, yes. Closure, no.
    – Michael K
    Commented May 1, 2012 at 20:18
  • NB in Python, methods as put in classes are just boring, regular functions (in Python 3.x, that is, in 2.x it's wrapped up in an "unbound method" that changes very little). Accessing them on an object gives you a "bound method", and that's like the method partially applied to the instance (it becomes the first parameter, hence the self parameter). That doesn't mean it's a closure though.
    – user7043
    Commented May 1, 2012 at 20:29
  • 3
    If we follow the definition given by Wikipedia (en.wikipedia.org/wiki/Closure_%28computer_science%29): "In computer science, a closure (also lexical closure or function closure) is a function together with a referencing environment for the non-local variables of that function. A closure allows a function to access variables outside its immediate lexical scope." Isn't this exactly the way in which methods access the member variables defined in the same object?
    – Giorgio
    Commented May 1, 2012 at 20:33
  • @delnan: If the object is passed as an explicit parameter like in Python I guess you are right. But apart from syntax, I was wondering whether a pair (method, object) is equivalent to a closure that uses the object's data as the environment for the non-local variables.
    – Giorgio
    Commented May 1, 2012 at 20:40

1 Answer 1

7

Yes, a method is a closure, from a different perspective. A closure is a function with state associated with it, and a method is a function with an object (state) associated with it. The difference is the paradigm. In object-oriented programming, a lot of emphasis is placed on the object itself, which the methods are part of. Whereas in functional programming (where the term "closure" comes from,) the emphasis is on the functions, that happen to have state that they close around.

It's quite possible to emulate objects in a functional language, by creating a list containing several closures that all close around the same state data. (This is how CLOS creates objects, IIRC.) And object-oriented that implement closures tend to make them work under the hood by having the compiler create special objects that implement the appropriate interface for the closure.

So yeah, the two concepts are quite equivalent; the only real difference is the paradigm.

5
  • Thanks for the answer, I will take a look at CLOS! So in a sense introducing closures in OO languages does not extend the semantics but only provides syntactic sugar for certain specific idioms (which can be useful, of course), and similarly, adding objects to a functional language like Lisp allows to capture certain programming idioms (a particular way of using closures) but does not extend the semantics of the language.
    – Giorgio
    Commented May 1, 2012 at 20:54
  • 1
    If you find CLOS interesting, I suggest reading "The Art of the Meta-Object Protocol" (en.wikipedia.org/wiki/The_Art_of_the_Metaobject_Protocol). For me it was a definite eureka moment.
    – Dibbeke
    Commented May 1, 2012 at 22:21
  • @Dibbeke: I have a lot in my reading queue but I hope I'll get to it soon. Thanks a lot!
    – Giorgio
    Commented May 4, 2012 at 17:55
  • Writing that comment, I decided to buy the book at Amazon, since it is such a classic. Read the book at the university 12 years ago.
    – Dibbeke
    Commented May 9, 2012 at 12:54
  • @Dibbeke: Maybe I will buy it too. It is just a pity that I have so little time to read or program. I mean, working as a full-time developer you want to do something else in your free time. :-) But I'll get to it sooner or later.
    – Giorgio
    Commented May 18, 2012 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.