"Closures are poor man's objects and vice versa" - What does this mean?

Functional ProgrammingObjectOopClosures

Functional Programming Problem Overview


> 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.

Functional Programming Solutions


Solution 1 - Functional Programming

> Objects are poor man's closures.

Consider Java. Java is an object-oriented programming language with no language level support for real lexical closures. As a work-around Java programmers use anonymous inner classes that can close over the variables available in lexical scope (provided they're final). In this sense, objects are poor man's closures.

> Closures are poor man's objects.

Consider Haskell. Haskell is a functional language with no language level support for real objects. However they can be modeled using closures, as described in this excellent paper by Oleg Kiselyov and Ralf Lammel. In this sense, closures are poor man's objects.


If you come from an OO background, you'll probably find thinking in terms of objects more natural, and may therefore think of them as a more fundamental concept than closures. If you come from a FP background, you might find thinking in terms of closures more natural, and may therefore think of them as a more fundamental concept than objects.

Moral of the story is that closures and objects are ideas that are expressible in terms of each other, and none is more fundamental than the other. That's all there is to the statement under consideration.

In philosophy, this is referred to as model dependent realism.

Solution 2 - Functional Programming

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(object):
    def __init__(self):
        self.breed = "Beagle"
        self.height = 12
        self.weight = 15
        self.age = 1
    def feed(self, amount):
        self.weight += amount / 5.0
    def grow(self):
        self.weight += 2
        self.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.

Solution 3 - Functional Programming

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.

Solution 4 - Functional Programming

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.

Solution 5 - Functional Programming

"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.

Solution 6 - Functional Programming

Just so much sugar, as closures hide anonymous objects under their skirts.

Solution 7 - Functional Programming

That objects can be used as a replacement for closures is quite easy to understand, you just place the captured state in the object and the calling as a method. Indeed for example C++ lambda closures are implemented as objects (things are sort of tricky for C++ because the language doesn't provide garbage collection and true closures with mutable shared state are therefore hard to implement correctly because of the lifetime of captured context).

The opposite (closures can be used as objects) is less observed but it's IMO a very powerful technique... consider for example (Python)

def P2d(x, y):
    def f(cmd, *args):
        nonlocal x, y
        if cmd == "x": return x
        if cmd == "y": return y
        if cmd == "set-x": x = args[0]
        if cmd == "set-y": y = args[0]
    return f

The function P2d returns a closure that captured the two values of x and y. The closure then provide access for reading and writing to them using a command. For example

p = P2d(10, 20)
p("x") # --> 10
p("set-x", 99)
p("x") # --> 99

so the closure is behaving like an object; moreover as any access is going through the command interface it's very easy to implement delegation, inheritance, computed attributes etc.

The nice book "Let Over Lambda" builds over this idea using Lisp as a language, but any language that supports closures can use this technique (in Lisp the advantage is that you can also bend the syntax using macros and read macros to improve usability and automatically generate all boilerplate code). The title of the book is exactly about this... a let wrapping a lambda:

(defun p2d (x y)
   (let ((x x) (y y))
     (lambda (cmd &rest args)
       (cond
          ((eq cmd 'x) x)
          ((eq cmd 'y) y)
          ((eq cmd 'set-x) (setq x (first args)))
          ((eq cmd 'set-y) (setq y (first args)))))))

Actually I'm not sure I agree with the "poor" adjective in this approach.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmissingfaktorView Question on Stackoverflow
Solution 1 - Functional ProgrammingmissingfaktorView Answer on Stackoverflow
Solution 2 - Functional ProgrammingBenTrofatterView Answer on Stackoverflow
Solution 3 - Functional ProgrammingP DaddyView Answer on Stackoverflow
Solution 4 - Functional ProgrammingcodenheimView Answer on Stackoverflow
Solution 5 - Functional ProgrammingChuckView Answer on Stackoverflow
Solution 6 - Functional ProgrammingDaGreekView Answer on Stackoverflow
Solution 7 - Functional Programming6502View Answer on Stackoverflow