From: Anton Moscal <msk@post.tepkom.ru>
To: Didier.Remy@inria.fr
Cc: caml-list@inria.fr
Subject: Re: Objects as sums
Date: Sat, 28 Nov 1998 13:46:12 +0300 (MSK) [thread overview]
Message-ID: <Pine.LNX.4.03.9811281337550.22023-100000@post.tepkom.ru> (raw)
In-Reply-To: <19981126165842.07547@morgon.inria.fr>
On Thu, 26 Nov 1998, Didier Remy wrote:
> class a = object (self : 'a)
> method b () = ((assert false): 'a)
> method c () = ((assert false): 'a)
> end;;
>
> Here, the methods b and c return an object of the same type as their own
> type. In particular, in a subclass, they will return an object of the type
> of objects of the subclass...
>
> Then, the rest of the example works unchanged.
>
> class c = object (self)
> inherit a
> method c () = self
> end;;
This is not solution of my task. I want to simulate by ocaml classes the
following C++ program:
# include <stdlib.h>
enum {NODE, LEAF};
class Node;
class Leaf;
class Tree { public:
virtual int tag () = 0;
virtual Node * node () { abort (); return 0; }
virtual Leaf * leaf () { abort (); return 0; }
};
class Leaf: public Tree { public:
int val;
int tag () { return LEAF; }
Leaf * leaf () { return this; }
};
class Node: public Tree { public:
Tree * l, * r;
int tag () { return NODE; }
Node * node () { return this; }
};
int sum (Tree * tree)
{
switch (tree -> tag ())
{
case LEAF: return tree -> leaf () -> val;
case NODE: return sum (tree -> node () -> l) + sum (tree -> node () -> r);
}
}
This program may be easily translated to ocaml as follows:
type tree = Leaf of int | Tree of tree * tree
let rec sum = function
Leaf v -> v
| Tree (l, r) -> sum l + sum r
but I would like to have solution with classes.
Your proposals do not work: type of (tree#node ()) will have `tree' type
and will fail on selection of a method, specific for node.
PS: this trick implements type-safe conversion down to objects hierarchy.
Question of my interest is the following: whether such
conversion is possible or not?
Regards,
Anton
next prev parent reply other threads:[~1998-11-30 10:22 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 ` Objects as sums Anton Moscal
1998-11-26 15:58 ` Didier Remy
1998-11-28 10:46 ` Anton Moscal [this message]
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.9811281337550.22023-100000@post.tepkom.ru \
--to=msk@post.tepkom.ru \
--cc=Didier.Remy@inria.fr \
--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