A-Posteriori Subtyping: Which Languages?

Does anyone know which OO languages support a-posteriori subtyping? In other words the creation of new subtype relationships between classes without modifying existing classes. Thanks a lot!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Views in Scala

I think Scala allows the user to define a view which is a dictionary to translate methods of one type to another.

The addition of views does not require altering the classes.

Thank you.

Thank you.

I'm assuming you mean...

creating a new supertype X of some class A, without modifying A to be a subtype of X. Creating a subtype of A without modifying A is easy. :)

Languages with structural subtyping and/or duck typing, of course, give this to you for free. I seem to recall at such a feature being considered for one of the major industrial statically-typed languages (Java?), but it may have been just a proposal, and my recollection may be rather hazy.

Also, anonymous type unions (algebraic sums) can be used to similar effect, in that the result of A + x is always a supertype of A.

Whoops! Yes of course.

Whoops! Yes of course.

you can modify the class

you can modify the class hierarchy with aspectj. for example, you can define an interface and then add that interface to existing classes. this does not require any modification of the existing classes, but is done during compilation, so you may need the source (although that may simply be an implementation choice; imagine the same transformations could be implemented by rewriting class files).

(aspectj adds "aspects" to java)

RIP TOM

TOM let you do that, I think, but the last update
on the website is 2001.

Sather

Sather is an offshoot of Eiffel, and allows you to insert novel classes at arbitrary points in the type lattice. This is essentially formalized duck typing, since Sather completely separates interface inheritance from implementation inheritance.

O'Caml

O'Caml, by virtue of supporting structural subtyping, does this, as does the aforementioned Sather.

In general, if you ask me, the languages to get to know really well today are Scala 2 and O'Caml.