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] functors and objects
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-02-04 (14:45)
From: Damien Pous <Damien.Pous@e...>
Subject: Re: [Caml-list] functors and objects'
En réponse à Matt Gushee <>:

> On Tue, Feb 03, 2004 at 07:06:35PM +0100, Damien wrote:
> > 	type t :> o (* sigh... *)
> The first problem is that you're mixing type declarations and class
> casts ... not a hard mistake to make, since the class cast syntax is a
> bit confusing, and doesn't seem to be explained very well anywhere.
of course, this was not "real OCaml code" :)
I just wanted to express the fact that my functor R has to accept any module M
defining an object type `t' with at least a method `react'
(that is, t :> o)

> > module R(M: O') = 
> > struct
> > 	include R(M)
> I don't see how this can work, because this module R takes an argument
> of type O', whereas the other R (do you really want to give them the
> same name?) takes an argument of type O. Regardless of their contents,
> they are different interfaces and thus not interchangeable. 
since o' :> o, M: O' should be a valid candidate for R...

> There's undoubtedly a way to achieve the result you want, but I don't 
> really understand what you are trying to do. 

the following correct code does partially what I want :

module type O = sig 
  type t
  val send: [<`React] -> t -> unit
module R(M: O) = struct
  let l = ref []
  let register (o: M.t) = l := o :: !l
  let react() = List.iter (M.send `React) !l

module type O' = sig
  type t
  val send: [<`React|`Render] -> t -> unit
module R'(M: O') = struct
  include R(M)   (* <-- "subtyping" is automagically done *)
  let render() = List.iter (M.send `Render) !l

but I am partially re-encoding objects with modules and variants,
and this encoding becomes quite ugly when it comes to _real_ objects
(with inheritance, virtual methods...)

> Do you actually have a specific goal in mind, or are you just experimenting?

both !

I am working on a project that use a "Reactor" where a lots of "Objects" react.
This project is cut into three layers :
  Base       reactor and objects (generic)
  Meaningful reactor and objects (objects really react)
  Rendered   reactor and objects (Gtk, maybe GL some day)

Currently both reactor and objects are "OCaml objects", but the project only use 
_one_ instance of the rendered reactor class. Thus I am wondering how I could
turn it into a module...
This module has to be defined incrementally : Base, Meaningful, Rendered.
but at the Base level, the final type of contained objects is unknown,
whence the need for a functor.
(with objects, this is done with parametrized classes)

Since objects really need to be "objects" (late binding), 
I cannot turn both objects and reactor into modules (or at least in a beautiful way)



To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: