[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: OO should break when broken (was re: the benefits of immutability)



On 2003.09.01 08:59 vkarvone@mappi.helsinki.fi wrote:

> 1. Derive the subtype relations between the types Real, Number, Complex,
>    and Integer?

Even this classic example can defy expectations.  In Bento I decided to 
support parametric polymorphism through the type system, with syntax
which allows an object to pick its implementation superclass at runtime,
depending on its parameters.  I was amazed to find out that this made
it as easy to use subclassing to generalize as to specialize.

Polymorphic supertyping lets you define a subclass which is effectively
a union of several types.  With such syntax it's easy to model a general
object such as Number as the polymorphic subclass of a range of specific
numeric types (e.g. Integer, Real and Complex), inheriting the value of
whichever superclass is selected based on an examination of parameter 
types at runtime.

The value of this approach is that your generalization can be tailor-made
to suit your needs; one program's idea of a generalized number may be
different than another's.

Another way to think of it is to consider subclassing as the direction
of dependency, not specialization.  Conventionally, subclassing is used
to define special types as refinements of general types, but in some 
cases it may make more sense to define a generalized type in terms of 
special types.  The result is a much more dynamic object hierarchy -- 
one which postpones the decision of where to inherit an object's 
implementation from until it's actually needed.

Michael

-----------------------------
Michael St. Hippolyte
http://www.bentodev.org