Re:Closures and Objects Are Equivalent (2013)
by jasonwatkinspdx on Sunday November 20, 2022 @06:39AM
(#33680154)
Delegation can model inheritance or other OOP semantics. If you squint your eyes delegation and lexical scope nesting kinda resemble each other.
Re:Closures and Objects Are Equivalent (2013)
by _old_dude_ on Sunday November 20, 2022 @09:01AM
(#33680936)
This is especially true in JavaScript. Inheritance between classes is implemented using delegation and the global scope is dynamic.
Re:Closures and Objects Are Equivalent (2013)
by zozbot234 on Sunday November 20, 2022 @09:25AM
(#33681111)
Not 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.
Re:Closures and Objects Are Equivalent (2013)
by jcelerier on Sunday November 20, 2022 @09:37AM
(#33681190)
Depends on which language, in c++ you can inherit from the type of a closure as it's just another object type
Re:Closures and Objects Are Equivalent (2013)
by int_19h on Sunday November 20, 2022 @05:05PM
(#33685931)
Not 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.
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.