views:

382

answers:

6

Closures are poor man's objects and vice versa.

I have seen this statement at many places on the web (including SO) but I don't quite understand what it means. Could someone please explain what it exactly means?

If possible, please include examples in your answer.

Thanks.

+2  A: 

See bendin's answer. I think the idea is that objects and closures accomplish the same thing.

EDIT: Oops, sorry. So, "Closures/objects are a poor man's objects/closures" is being used in that parable to point out that the two are one in the same (?). So, the real question is "what's the difference between objects and closures?"?

Is this at all what you're looing for? http://groups.google.com/group/comp.lang.functional/msg/88d65e28baafeadd?pli=1

Matt Blaine
The second link in my post links to the same paragraph.
missingfaktor
+6  A: 

An object, at its simplest, is just a collection of state and functions that operate on that state. A closure is also a collection of state and a function that operates on that state.

Let's say I call a function that takes a callback. In this callback, I need to operate on some state known before the function call. I can create an object that embodies this state ("fields") and contains a member function ("method") that performs as the callback. Or, I could take the quick and easy ("poor man's") route and create a closure.

As an object:

class CallbackState{
    object state;

    public CallbackState(object state){this.state = state;}

    public void Callback(){
        // do something with state
    }
}

void Foo(){
    object state = GenerateState();
    CallbackState callback = new CallbackState(state);
    PerformOperation(callback.Callback);
}

This is pseudo-C#, but is similar in concept to other OO languages. As you can see, there's a fair amount of boilerplate involved with the callback class to manage the state. This would be much simpler using a closure:

void Foo(){
    object state = GenerateState();
    PerformOperation(()=>{/*do something with state*/});
}

This is a lambda (again, in C# syntax, but the concept is similar in other languages that support closures) that gives us all the capabilities of the class, without having to write, use, and maintain a separate class.

You'll also hear the corollary: "objects are a poor man's closure". If I can't or won't take advantage of closures, then I am forced to do their work using objects, as in my first example. Although objects provide more functionality, closures are often a better choice where a closure will work, for the reasons already stated.

Hence, a poor man without objects can often get the job done with closures, and a poor man without closures can get the job done using objects. A rich man has both and uses the right one for each job.

P Daddy
+5  A: 

EDITED: The title of the question does not include "vice versa" so I'll try not to assume the asker's intent.

The two common camps are functional vs imperative languages. Both are tools that can accomplish similar tasks in different ways with different sets of concerns.

Closures are poor man's objects.

Objects are poor man's closures.

Individually, each statement usually means the author has a some bias, one way or another, usually rooted in their comfort with one language or class of language vs discomfort with another. If not bias, they may be constrained with one environment or the other. The authors I read that say this sort of thing are usually the zealot, purist or language religious types. I avoid the language religious types if possible.

Closures are poor man's objects. Objects are poor man's closures.

The author of that is a "pragmatist" and also pretty clever. It means the author appreciates both points of view and appreciates they are conceptually one and the same. This is my sort of fellow.

mrjoltcola
I think you missed the "and vice versa" part of the statement.
Chris Conway
No, I didn't miss it, I just misread it. I just wasn't sure if the asker meant it was actually part of the quote, or if he meant he saw both versions of a quote. Now that you point it out, I see what you mean, the author of the statement is likely UN-biased, appreciating both sides, but probably mediating a discussion between some "zealous, purist, language religious types".
mrjoltcola
The title of the question is "Closures are a poor man's objects" without the "vice versa", so I'm not sure which one the asker is asking. I obviously answered the title, and possibly not the body. Oh well.
mrjoltcola
+12  A: 

The point is that closures and objects accomplish the same goal: encapsulation of data and/or functionality in a single, logical unit.

For example, you might make a Python class that represents a dog like this:

class Dog():
    breed = "Beagle"
    def __init__(self):
        height = 12
        weight = 15
        age = 1
    def feed(self, amount):
        weight += amount / 5.0
    def grow(self):
        weight += 2
        height += .25
    def bark(self):
        print "Bark!"

And then I instantiate the class as an object

>>> Shaggy = Dog()

The Shaggy object has data and functionality built in. When I call Shaggy.feed(5), he gains a pound. That pound is stored in variable that's stored as an attribute of the object, which more or less means that it's in the objects internal scope.

If I was coding some Javascript, I'd do something similar:

var Shaggy = function() {
    var breed = "Beagle";
    var height = 12;
    var weight = 15;
    var age = 1;
    return {
        feed : function(){
            weight += amount / 5.0;
        },
        grow : function(){
            weight += 2;
            height += .25;
        },
        bark : function(){
            window.alert("Bark!");
        },
        stats : function(){
            window.alert(breed "," height "," weight "," age);
        }
    }
}();

Here, instead of creating a scope within an object, I've created a scope within a function and then called that function. The function returns a JavaScript object composed of some functions. Because those functions access data that was allocated in the local scope, the memory isn't reclaimed, allowing you to continue to use them through the interface provided by the closure.

OlduvaiHand
+2  A: 

"Objects are a poor man's closures" isn't just a statement of some theoretical equivalence — it's a common Java idiom. It's very common to use anonymous classes to wrap up a function that captures the current state. Here's how it's used:

public void foo() {
    final String message = "Hey ma, I'm closed over!";
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            System.out.println(message);
        }
    });
}

This even looks a lot like the equivalent code using a closure in another language. For example, using Objective-C blocks (since Objective-C is reasonably similar to Java):

void foo() {
    NSString *message = @"Hey ma, I'm closed over!";
    [[NSOperationQueue currentQueue] addOperationWithBlock:^{
        printf("%s\n", [message UTF8String]);
    }];
}

The only real difference is that the functionality is wrapped in the new Runnable() anonymous class instance in the Java version.

Chuck