From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id NAA05207; Mon, 23 Apr 2001 13:52:26 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id NAA05342 for caml-list@pauillac.inria.fr; Mon, 23 Apr 2001 13:52:25 +0200 (MET DST) Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id FAA23853 for ; Sun, 22 Apr 2001 05:44:18 +0200 (MET DST) Received: from relay.EECS.Berkeley.EDU (relay.EECS.Berkeley.EDU [169.229.34.228]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id f3M3iGH28855 for ; Sun, 22 Apr 2001 05:44:16 +0200 (MET DST) Received: from eecs.berkeley.edu (frogstar.EECS.Berkeley.EDU [128.32.171.238]) by relay.EECS.Berkeley.EDU (8.9.3/8.9.3) with ESMTP id UAA01756; Sat, 21 Apr 2001 20:44:15 -0700 (PDT) Message-ID: <3AE2538F.83A03652@eecs.berkeley.edu> Date: Sat, 21 Apr 2001 20:44:15 -0700 From: Gregoire Sutre X-Mailer: Mozilla 4.77 [en] (X11; U; Linux 2.2.18-ky1 i686) X-Accept-Language: en MIME-Version: 1.0 To: caml-list@inria.fr Subject: [Caml-list] Open class type definition ? Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Hi, I'm trying to use the Object model of OCaml and I'm having trouble with the class types. My goal is to have datatypes that can be easily converted into strings for debugging purposes. So instead of enclosing each datatype inside a dedicated module with a to-string() function (that would lead to a huge number of modules), I thought that I could use an object type instead, saying that there is *at least* a method to_string(), and define my datatypes as classes with *at least* a to_string() method inside each class. For instance, I could specify an ordered type this way: module type ORDERED_TYPE = sig type t = < to_string : unit -> string > val compare : t -> t -> int end This would ensure that the type t has a to_string() method, and then I could have the following functor: module Funct(A : ORDERED_TYPE) = struct let display_cmp x y = x#to_string() ^ " cmp " ^ x#to_string() ^ " = " ^ (string_of_int (A.compare x y)) end where I could use to_string(). The problem is that the declaration for type t above: type t = < to_string : unit -> string > actually enforces objects to have only the to_string method and no other public methods. But the compare function may need to read the contents of the objects, and for that it would need public accessors. Consider for instance the following module: module Foo = struct class my_int x = object val mutable v = x method to_string () = "Int(" ^ (string_of_int v) ^")" method get_v = v method set_v new_v = v <- new_v end type t = my_int let compare (x:t) (y:t) = compare x#get_v y#get_v end In this module, the compare function reads the integer value stored in the objects to perform its job. But then, the type t of the Foo module does not correspond to the type t of the ORDERED_TYPE signature and I can not apply the functor to Foo: [...] Type declarations do not match: type t = my_int is not included in type t = < to_string : unit -> string > So I tried to define the type t in the signature so that it does not enforce objects to have only the to_string method. I tried the following open type: type t = < to_string : unit -> string; .. > but I get the error: Unbound type parameter <..> I also tried the # construct, without success: # class type class_t = object val to_string : unit -> string end;; class type class_t = object val to_string : unit -> string end # type t = #class_t;; Unbound row variable in #class_t Is it possible to define such open class types. And if not then why is it so ? Greg. -- Gregoire Sutre 144MB Cory Hall sutre@eecs.berkeley.edu University of California Phone: (510) 643-2801 Berkeley, CA 94720 ------------------- To unsubscribe, mail caml-list-request@inria.fr. Archives: http://caml.inria.fr