"Closing is bad human objects and vice versa." What does it mean?

Closing is bad human objects and vice versa.

I have seen this statement in many places on the Internet ( including SO ), but I do not quite understand what this means. Can someone explain what this means?

If possible, provide examples in your answer.

+49
closures object oop functional-programming
Mar 23 '10 at 5:38
source share
6 answers

Objects are bad circuits of a person.

Consider Java. Java is an object-oriented programming language without language support for real lexical closures. How Java programmers use anonymous inner classes that can close variables available in the lexical scope (provided they are final ). In this sense, objects are bad circuits of a person.

Closing is bad human objects.

Consider Haskell. Haskell is a functional language without language level support for real objects. However, they can be modeled using closures, as described in this excellent article by Oleg Kiselev and Ralph Lummel. In this sense, closure is bad human objects.




If you come from the background of OO, you are likely to find thinking in terms of objects more natural, and therefore you can think of them as a more fundamental concept than closing. If you come from the background of FP, you can find thinking in terms of more natural closures and, therefore, think of them as a more fundamental concept than objects.

The moral of history is that closures and objects are ideas that are expressed with each other, and none of them is more fundamental than the other . That’s all for the statement in question.

In philosophy, this is called a model-specific implementation .

+43
Jul 10 2018-12-12T00:
source share

The fact is that closures and objects fulfill the same goal: encapsulating data and / or functionality into a single logical unit.

For example, you can create 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 built-in data and functionality. When I call Shaggy.feed(5) , he gets a pound. This pound is stored in a variable, which is stored as an attribute of an object, which more or less means that it is in the inner area of ​​the objects.

If I encoded some Javascript, I would do something like this:

 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 region inside an object, I created a region inside a function, and then called this function. The function returns a JavaScript object consisting of some functions. Since these functions gain access to data that has been allocated in the local area, memory is not restored, which allows you to continue to use them through the interface provided by the closure.

+51
Mar 23 '10 at 6:36
source share

An object, in its simplest form, is simply a collection of states and functions that work in this state. Closing is also a collection of states and a function that works in this state.

Let's say I call a function that performs 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 a callback function. Or, I could take a quick and easy ("poor man") route and create a closure.

Like 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 in a different way similar to other OO languages. As you can see, there are enough templates associated with the callback class to manage the state. This would be a lot easier with closure:

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

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

You will also hear the consequence: "objects are poor people." If I cannot or will not use closure, then I am forced to do my work using objects, as in my first example. Although objects provide more functionality, closing is often the best choice when closing will work for reasons already stated.

Therefore, a poor person without objects can often do work with closure, and a poor person without closure can do his job using objects. A rich person has both, and uses the right one for each task.

+12
Mar 23 '10 at 6:12
source share

EDITED: The title of the question does not include β€œvice versa,” so I will try not to suggest the intent of a scam.

Two common camps are functional and imperative languages. Both are tools that can perform similar tasks in different ways with different problems.

Closing is bad human objects.

Objects are bad circuits of a person.

Individually, each statement usually means that the author has some bias, one way or another, as a rule, rooted in their comfort with one language or class of language and discomfort with another. If this is not an offset, they may be limited to one environment or another. The authors I read say that such things are usually fanatics, fanatics, purists, or languages. I avoid religious language types, if possible.

Closing is bad human objects. Objects are bad circuits of a person.

The author of this "pragmatist", and also quite smart. This means that the author appreciates both points of view and appreciates that they are conceptually the same thing. This is my friend.

+6
Mar 23 '10 at 6:31
source share

β€œObjects are closed people” - this is not just a statement of some theoretical equivalence - it is a common Java idiom. Very often, anonymous classes are used to complete a function that captures the current state. Here's how it is used:

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

It even looks a lot like equivalent code using closure in another language. For example, using Objective-C blocks (since Objective-C is pretty 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 completed in an instance of the anonymous class new Runnable() in the Java version.

+5
Mar 23 '10 at 18:34
source share

There is just so much sugar as the closure hides anonymous objects under the skirts.

+5
Nov 08 '10 at 4:06
source share



All Articles