Claude Pache claude.pache at gmail.com
Sat Jun 29 10:55:02 PDT 2013



Le 29 juin 2013 à 18:28, Eric Elliott <eric at ericleads.com> a écrit :

> "it could be quickly refactored as following"
> 
> Except that a bunch of animals already depend on Swimming, and now they all need to be refactored as well, or you have to duplicate Swimming functionality in more than one place, and then you have unexpectedly divergent implementations across different animals (maybe a bug in one type of swimming animal that doesn't exist in another, or bugs in both, you fix it in one place, think it's fixed everywhere, only to discover later that it wasn't.)

Uh, I have tried to be ingenious enough, precisely so that none of these problems arises: No need to refactor existing code using Swimming *and* no duplication of the functionality of Swimming.

—Claude

> 
> This is the famous unexpected divergence problem that arises from single ancestor class inheritance... so even if you start out using class inheritance, it can be problematic to switch to mixins and similar strategies down the road.
> 
> - Eric
> 
> 
> On Fri, Jun 28, 2013 at 7:41 PM, Claude Pache <claude.pache at gmail.com> wrote:
>> 
>> 
>> Le 29 juin 2013 à 00:14, Eric Elliott <eric at ericleads.com> a écrit :
>> 
>>> <snip>
>>> 
>>> I saw a recent example of one of the problems with single-ancestor inheritance in a talk. I wish I could remember which one. The illustrations were great:
>>> 
>>> Animal
>>>   - Walking
>>>     - Monkey
>>>     - Human
>>>   - Flying
>>>     - Bird
>>>     - Bee
>>>   - Swimming
>>>     - Fish
>>>     - Whale
>>> 
>>> Now we need alligator and duck.
>> 
>> Interesting challenge! It may be hard to resolve in some languages, but in JavaScript, you merely need some imagination. Given:
>> 
>> ```js
>> class Flying extends Animal {
>>     // implementation of Flying
>> }
>> 
>> class Swimming extends Animal {
>>     // implementation of Swimming
>> }
>> ```
>> 
>> it could be quickly refactored as following (I assume that the implementation of Swimming is sufficiently well-written; otherwise some amendments in its implementation might be needed):
>> 
>> ```js
>> class Flying extends Animal {
>>     // implementation of Flying
>> }
>> 
>> function addSwimming(Animal) {
>>     class Swimming extends Animal {
>>         // implementation of Swimming
>>     }
>>     return Swimming
>> }
>> 
>> var Swimming = addSwimming(Animal)
>> 
>> var FlyingSwimming = addSwimming(Flying)
>> 
>> class Duck extends FlyingSwiming { }
>> ```
>> 
>> (It is true that `Duck instanceof Swimming` doesn't hold; however, duck-typing forbids the use of `instanceof` and even any typelike-check.)
>> 
>> Note how JavaScript remains dynamic, even in the face of the ES6-class-sugar.
>> 
>> 
>> —Claude
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130629/7489ac17/attachment-0001.html>


More information about the es-discuss mailing list