caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Richard Jones <rich@annexia.org>
To: caml-list@inria.fr
Subject: Why can't immediate objects be extended?
Date: Sat, 12 Jul 2008 15:12:00 +0100	[thread overview]
Message-ID: <20080712141200.GA8634@annexia.org> (raw)


I'm wondering if there's a reason why one cannot inherit from
immediate objects? (See description at:
http://caml.inria.fr/pub/docs/manual-ocaml/manual005.html#ss:immediate-objects)
Is it just a syntax problem because the 'inherit' keyword currently
needs to take a class type, or is there a deeper reason?

This is the code I'd like to write (non-working, obviously):

  type 'a tree = Leaf of string | Node of 'a tree * 'a * 'a tree
  
  let none = object end
  
  let tree = Node (Leaf "1", none, Leaf "2")
  
  let rec string_of_tree = function
    | Leaf str -> str
    | Node (left, _, right) ->
        "(" ^ string_of_tree left ^ "," ^ string_of_tree right ^ ")"
  
  let rec annotate = function
    | (Leaf _ as t) -> t
    | (Node (left, parent_obj, right) as t) ->
        let obj = object
  	  inherit (typeof parent_obj)
  	  method str = string_of_tree t
        end in
        Node (annotate left, obj, annotate right)

  let tree = annotate tree

The problematic function is 'annotate'.  I believe I want annotate to
have a type along these lines (again, this is not precisely OCaml code
because I've used alpha to stand for '..'):

  val annotate : < 'a > tree -> < str : string; 'a > tree

Rich.

-- 
Richard Jones
Red Hat


             reply	other threads:[~2008-07-12 14:12 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-07-12 14:12 Richard Jones [this message]
2008-07-12 14:26 ` [Caml-list] " Richard Jones
2008-07-14  1:47 ` Peng Zang
2008-07-14 14:38 ` Keiko Nakata
2008-07-16  1:43 ` Jacques Garrigue

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=20080712141200.GA8634@annexia.org \
    --to=rich@annexia.org \
    --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;
as well as URLs for NNTP newsgroup(s).