Re: OCaml's subtyping better than advertised?

From: Wolfgang Lux (lux@heidelbg.ibm.com)
Date: Wed May 21 1997 - 17:03:32 MET DST


Message-Id: <9705211403.AA33541@idse.heidelbg.ibm.com>
To: Donald Syme <Donald.Syme@cl.cam.ac.uk>
Subject: Re: OCaml's subtyping better than advertised?
In-Reply-To: (Your message of Tue, 20 May 97 15:22:28 N.)
             <E0wTpo6-0001pa-00@heaton.cl.cam.ac.uk>
Date: Wed, 21 May 97 16:03:32 +0100
From: Wolfgang Lux <lux@heidelbg.ibm.com>

> One thing I'm wondering is why the syntax for O'Caml types has no
> way of specifying interface inheritance easily? For
> example, I want to define some object type (nb. I'm defining
> a type not a class):
>
> type widget = < windowid: unit -> int,
> set_height: int -> unit,
> height: unit -> int,
> .. >;
>
> [ Aside: at the moment I don't seem to be able to do this
> in O'Caml, without defining a class, because the type
> variable ".." is free. Perhaps there needs to be come
> mechanism for defining new object types with the same
> "#<type>" behaviour of the types generated by classes?
> This would then allow me to write "#widget" as a type
> without having defined a class widget (i.e. without
> having committed to an implementation) ]
>

This *can* easily be done by using virtual methods, e.g.

virtual class widget () =
  virtual windowid: int (* unit -> ... is not necessary here! *)
  virtual set_height: int -> unit
  virtual height: int (* unit -> ... not necessary as well *)
end

> If I then want to define some structural subtype of #widget
> (nb. not necessarily implemented by a subclass), I have to write out all
> the methods again:
>
> type textual_widget = < windowid: unit -> int,
> set_height: int -> unit,
> height: unit -> int,
> insert : string * int -> unit,
> get : int * int -> string
> .. >;
>

This is easily done as well:

virtual class textual_widget () =
  inherit widget ()
  virtual insert : string -> int -> unit
  virtual get : int -> int -> string
end

>
> Yours,
> Don Syme

Regards
Wolfgang

>
> P.S. Apologies for no french version.
Apologies from me as well.

----
Wolfgang Lux                     WZH Heidelberg, IBM Germany
Phone: +49-6221-59-4546                Fax: +49-6221-59-3500
Internet: lux@heidelbg.ibm.com          Office: mazvm01(lux)



This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:11 MET