Version française
Home     About     Download     Resources     Contact us    
Browse thread
Local opening of modules
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Anton Moscal <msk@p...>
Subject: Objects as sums
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