caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Why can't immediate objects be extended?
@ 2008-07-12 14:12 Richard Jones
  2008-07-12 14:26 ` [Caml-list] " Richard Jones
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Richard Jones @ 2008-07-12 14:12 UTC (permalink / raw)
  To: caml-list


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


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2008-07-16  1:43 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-07-12 14:12 Why can't immediate objects be extended? Richard Jones
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

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).