Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Open class type definition ?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Gregoire Sutre <sutre@e...>
Subject: [Caml-list] Open class type definition ?

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 =
    type t = < to_string : unit -> string >
    val compare : t -> t -> int

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) =
    let display_cmp x y = x#to_string() ^ " cmp " ^ x#to_string() ^
                          " = " ^ (string_of_int ( x y))

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 =
    class my_int x =
        val mutable v = x
        method to_string () = "Int(" ^ (string_of_int v) ^")"
        method get_v = v
        method set_v new_v = v <- new_v

    type t = my_int
    let compare (x:t) (y:t) = compare x#get_v y#get_v

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

Is it possible to define such open class types.  And if not then why is it
so ?


Gregoire Sutre                      144MB Cory Hall             University of California
Phone: (510) 643-2801               Berkeley, CA 94720
To unsubscribe, mail  Archives: