Version française
Home     About     Download     Resources     Contact us    
Browse thread
functors vs classes
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Wolfgang Lux <lux@h...>
Subject: Re: OCaml's subtyping better than advertised?
> 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)