caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: james woodyatt <jhw@wetware.com>
To: Gerd Stolpmann <info@gerd-stolpmann.de>
Cc: The Trade <caml-list@inria.fr>
Subject: Re: [Caml-list] Recursive classes are impossible?
Date: Mon, 24 Jun 2002 16:55:45 -0700	[thread overview]
Message-ID: <E616A93E-87CD-11D6-BC7A-000502DB38F5@wetware.com> (raw)
In-Reply-To: <20020624233419.C760@ice.gerd-stolpmann.de>

On Monday, June 24, 2002, at 02:34 PM, Gerd Stolpmann wrote:
>
> No, this is not the problem. Try the following:
>
> class tree =
> object(self:'self)
>   val mutable v = (None : int option)
>   val mutable ch = ([] : 'self list)
>   method get = v
>   method set x = v <- x
>   method children = ch
>   method set_children x = ch <- x
> end
>
> This definition works at the first glance, but if you try to
> inherit from this class and subtype this class at the same
> time, you will run into problems.
>
> class broccoli =
> object
>   inherit tree
>   method color = "green"
> end

Here's a more graphic explanation of why this is a bad idea:

	let t = new tree;;
	let b = new broccoli;;
	(b :> tree)#set_children [t];;

Here, the compiler-- quite sensibly-- errors on the coercion.

The "set_children" method of class tree takes a list of tree objects.  
The "set_children" method of class broccoli takes a list of broccoli 
objects.  If you were able to coerce a broccoli object into a tree 
object, you could then set its children to a list of non-broccoli tree 
objects-- which would violate the invariant for objects of the broccoli 
class.


--
j h woodyatt <jhw@wetware.com>

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  reply	other threads:[~2002-06-24 23:53 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-06-24 13:59 Alessandro Baretta
2002-06-24 21:34 ` Gerd Stolpmann
2002-06-24 23:55   ` james woodyatt [this message]
2002-06-25 23:48   ` Alessandro Baretta
2002-06-26  2:43     ` John Prevost

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=E616A93E-87CD-11D6-BC7A-000502DB38F5@wetware.com \
    --to=jhw@wetware.com \
    --cc=caml-list@inria.fr \
    --cc=info@gerd-stolpmann.de \
    /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;
as well as URLs for NNTP newsgroup(s).