Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Anton Moscal <msk@post.tepkom.ru>
To: caml-list@inria.fr
Subject: Objects as sums
Date: Wed, 25 Nov 1998 18:17:35 +0300 (MSK)	[thread overview]
Message-ID: <Pine.LNX.4.03.9811251811020.1044-100000@post.tepkom.ru> (raw)
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




  reply	other threads:[~1998-11-26 15:27 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-10-27 20:35 Local opening of modules John Prevost
1998-11-10 16:04 ` Anton Moscal
1998-11-10 21:22   ` John Prevost
1998-11-25 15:17     ` Anton Moscal [this message]
1998-11-26 15:58       ` Objects as sums Didier Remy
1998-11-28 10:46         ` Anton Moscal
1998-11-30 12:35           ` Jerome Vouillon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.03.9811251811020.1044-100000@post.tepkom.ru \
    --to=msk@post.tepkom.ru \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox