"But Smalltalk's mechanism suffers from a serious drawback: Initialization is not enforced. new creates objects but does not initialize them... A second drawback with both Smalltalk and Ruby is that initialize, being an ordinary method, does not chain: You must remember to begin your initialize methods with a call to the superclass's initialize method."Hey, but this just plain, old message sending, as with any object. It just happens that that "object" is a class. That's the advantage, and the tradeoff.
So, in general, inheritance is not as good as composition. But why is *this* message sequence worse than any other?
Do objects have too much freedom?
1 comment:
Contracts would also address this, as the underlying criticism seems to be that there's a contract (perhaps implicit) that any instance of a class will be initialized in some way -- including in subclasses. Maybe it's even valid to have a contract that a certain section of code (a method definition for a particular class) will be run on instance creation (that would be a contract that addressed future compatibility in addition to current functionality).
A contract probably won't be statically checked. But it would probably express the requirement well, and provide early warning of object misuse.
Post a Comment