Class Isomorphisms

ClassIsomorphisms is an idea to improve class reusability in the ObjectOriented paradigm.

ThinkingOutLoud GunnarZarncke 2013.

Motivating example (pseudo-language):

  class CartesionPoint? { 
    double x; 
    double y;
    CartesianPoint? move(CartesianPoint? delta) { ... }
  }

class PolarPoint? isomorphic CartesianPoint? { double r; double phi;

PolarPoint? rotate(double alpha) { ... }

CartesianPoint? to() { return new CartesionPoint?(r*cos(phi),r*sin(phi)); } PolarPoint?(CartesianPoint? p) { r = sqrt(p.x*p.x+p.y*p.y); phi = atan(p.x/p.y); } } }

CartesianPoint? p = new Cartesion(1,1); p = p.move(new Polar(1,0)).rotate(pi);

This example assumes that somebody implemented and used CartesionPoint?. And somebody else later added PolarPoint? which describes the same thing - a point - in another way, but may provide different methods (here rotation which is not so easy in cartesian coordintes). The keyword isomorphic indicates to the compiler that both representations are isomorphic and can be used interchangeably (provided suitable conversions/"casts" are given as is the here here).

Note that this differs from "extends" which allows only to use one of PolarPoint? p = new CartesionPoint?, CartesionPoint? p = new PolarPoint?, whereas the isomorphism allows to use both!

I know that this is basically identical to what can be done with ScalaLanguage ImplicitConversions?. But then I had this idea in 1996 during a C++ course long before Scala. And I imagine the isomorphism semantics is be stronger than that of Scalas implicits which can do anything and loose information.

In Scala it is discouraged to use transitive implicit conversions because this leads to "converting anything to anything" which is deemed inmaintainable. This is because implicits allow down and up conversions, which this isomorphism concept explicitly disallows.

One area where this would prove helpful is the dreaded interface extension problem where you want to extend a class (to which you have no access) with helper functions (think of adding append to String) and be able to use these added methods everywhere where you have only the original object.

What do you think? -- GunnarZarncke


EditText of this page (last edited May 21, 2013) or FindPage with title or text search