Objects as sums

From: Anton Moscal (msk@post.tepkom.ru)
Date: Wed Nov 25 1998 - 16:17:35 MET


Date: Wed, 25 Nov 1998 18:17:35 +0300 (MSK)
From: Anton Moscal <msk@post.tepkom.ru>
To: caml-list@inria.fr
Subject: Objects as sums
In-Reply-To: <Pine.LNX.3.95L.981110161850.2472G-100000@naiad.fac.cs.cmu.edu>

Hello!

I try to write in O'Caml a well-known idiom of OO for simulating disjoint
sum of types by virtual methods and inheritance: working example in C++:

+++++++++
#include <stdlib.h>
class B; class C;
class A { public:
  virtual B * b () { abort (); return 0; }
  virtual C * c () { abort (); return 0; }
};

class B { public: virtual B * b () { return this; } };
class C { public: virtual C * c () { return this; } };
--------
but, when I write the same program in O'Caml:
++++++++
class a = object (self)
  method b () = ((assert false): b)
  method c () = ((assert false): c)
end

and b = object (self)
  inherit a
  method b () = self
end

and c = object (self)
  inherit a
  method c () = self
end
-------
It doesn't work (with diagnostics `self type can't escape from it's
scope'). The following question arises: either this idiom can't be
expressed in O'Caml or I'm fool :) and is it possible to do it by any way?

Regards,
Anton E. Moscal



This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:16 MET