Questions about class types

Hendrik Tews (tews@tcs.inf.tu-dresden.de)
Thu, 21 Nov 1996 14:57:42 +0100

Date: Thu, 21 Nov 1996 14:57:42 +0100
Message-Id: <199611211357.OAA10366@ithif18.inf.tu-dresden.de>
From: Hendrik Tews <tews@tcs.inf.tu-dresden.de>
To: caml-list@margaux.inria.fr
Subject: Questions about class types

Hello,

I want to write a module which provides a class with some
features, say

#module type A = sig
# class b (unit)= method init : unit
# end
#end;;

but I want to implement it via a class with more features, say

#module B = struct
# class b () as self =
# val a = ()
# method init = self#f
# method f = a
# end
#end;;

Unfortunately B does not match A, because (as it is written in
the documentation under 4.9.2) one can add additional instance
variables, but no additional methods. What is the reason for
this? As you can guess I would like additional methods to get
hidden when matching a class against a class type.

The first idea is to implement f as an instance variable (of
functional type) as well, but this gets very ugly as soon as
there are some mutually recursive f's.

The next idea was to specify only a type and some
functionality, not a class ie:

#module type A' = sig
# val newb : unit -> < init : unit; .. >
#end;;
#
#module B' : A' = struct
# class b () as self =
# val a = ()
# method init = self#f
# method f = a
# end
#
# let newb () = new b ()
#end;;

The disadvantage of this approach would be that a user of A'
can't use new, because he does not have access to the class.
To my surprise it did not type check, because

Values do not match:
val newb : unit -> b
is not included in
val newb : unit -> < init : unit; .. >

Even with explicit typing newb, ie substituting

# let newb () : < init : unit; .. > = ((new b ()) : < init : unit; .. >)

I get the same error. So far I thought, that a class type is only
a abbreviation for something like < .. >. Where am I wrong?

Bye,

Hendrik Tews

-------------------------------------------------------------
e-mail: tews@tcs.inf.tu-dresden.de
www: http://www.inf.tu-dresden.de/~ht1
Console: ithif18.inf.tu-dresden.de