5 ms·
Closures don’t have inheritance or any of the other machinery that object oriented programming typically comes with. Perhaps saying they are light weight objec
by rosebay 4y ago
Closures don’t have inheritance or any of the other machinery that object oriented programming typically comes with.
Perhaps saying they are light weight objects (where objects has its meaning as instances of classes) is a good way of saying it.
The realisation that closures and objects are very similar is one of the main turning points in becoming proficient in both OOP and Functional Programming, it was for me at least.
- jasonwatkinspdx 4y agoDelegation can model inheritance or other OOP semantics. If you squint your eyes delegation and lexical scope nesting kinda resemble each other.
- _old_dude_ 4y agoThis is especially true in JavaScript. Inheritance between classes is implemented using delegation and the global scope is dynamic.
- zozbot234 4y agoNot quite. Inheritance needs a "tying the knot" construction, because a base-class method can call a method that may have been overridden in a derived class. So you need an indirection step for any call to an overridable method, which cannot be modeled by simple delegation. Of course this is really a misfeature (outside of pure abstract base classes that, like interfaces or traits, don't implement their defined methods), which is at the root of the well-known "fragile base class" problem. It means implementation-inheritance is inherently non-modular, even though literally everything else in OOP was specifically intended to enable and promote modularity.
- jcelerier 4y agoDepends on which language, in c++ you can inherit from the type of a closure as it's just another object type
- int_19h 4y agoNot all OO languages even have inheritance. I would argue that the only two features that are mandatory to call something "object-oriented" is 1) the notion of object identity, and 2) some kind of dynamic method dispatch.
- rosebay 4y agoI would agree