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

后端 未结 6 956
囚心锁ツ
囚心锁ツ 2020-11-28 02:48

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

6条回答
  •  一整个雨季
    2020-11-28 03:09

    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.

提交回复
热议问题