Version française
Home     About     Download     Resources     Contact us    
Browse thread
Why can't immediate objects be extended?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Richard Jones <rich@a...>
Subject: Why can't immediate objects be extended?

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