Hacker News new | ask | show | jobs
by Mikhail_Edoshin 31 days ago
Thanks, will add that to my reading list.

Inheritance is both good and not so good. The problem it solves is in a way unique and the very fact that it solves it is indeed quite a feat. But the solution is rather crude.

In a comment nearby I put out a theory that an object is essentially a managed computation, a computation that is driven by external events. What inheritance does is that it allows us to meld two or more such computations together in a relatively seamless way.

Again, a simple but maybe not a minimal model could be that. We have a set of collections: linked list, queue, AVL tree, etc. Most of them do not have a built-in way to count the number of objects they contain. Some do; e.g. an array, but many do not. But, of course, it is not that hard to add it to any collection. Assuming we have 'Init', 'AddElem' and 'RemElem' all we have to do is to add a counter that is set to 0 at 'Init', increment on 'AddElem' and decrement on 'RemElem'. Then we could read the current value with 'NumElems'. At the same time if we do not need a counter, then we should not add it to avoid extra work. So it looks like it would be nice to somehow extract the idea of a counter into a separate computation and then add it to a collection as necessary.

Inheritance allows us to do that in a uniform and general way and this is surely a remarkable achievement. But its solution is not simple. E.g. I have no idea what is the best way to inherit here and am afraid we'll end up with a separate variation for each collection. Yet the concept of what we are after is basically that:

    aaaa = new LinkedList;
    bbbb = new LinkedList + Counter;
Of course it may require special definitions at the class level, but otherwise the result should be that simple, because these are all the distinctions that are important in this context.