Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] functors and objects
From: Damien <Damien.Pous@ens-lyon.fr>

> I would like to write something like
> 
> <<
> class type o = 
> object 
> 	method react: unit
> end
> 
> module type O = 
> sig
> 	type t :> o (* sigh... *)
> end
> 
> module R(M: O) = 
> struct
> 	let l: M.t list ref = []
> 	let register (o: M.t) = l := o :: !l
> 	let react() = List.iter (fun o -> o#react) !l
> end
> >>

The closest I can see to what you ask for is

module type O = sig
  type t
  val as_o : t -> o
end

module R(M: O) = struct
  let l: M.t list ref = ref []
  let register (o: M.t) = l := o :: !l
  let react() = List.iter (fun o -> (M.as_o o)#react) !l
end

module RO = R(struct type t = o let as_o x = x end)

Then your second layer would be

class type o' = object 
  inherit o
  method render: unit
end

module type O' = sig
  include O
  val as_o' : t -> o'
end

module R(M: O') = struct
  include R(M)
  let render() = List.iter (fun o -> (M.as_o' o)#render) !l
end

module O' = struct
  type t = o'
  let as_o  x = (x : t :> o)
  let as_o' x = x
end

module RO' = R(O')

> Is it unsound to let a functor use an object type ?
> (not to inherit from the class, 
> just to use the methods of objects belonging to this type)

This isn't a problem of soundness.
There is just no such thing as a "partially abstract" object type.
But as shown above, you can easily simulate it by coupling an abstract
type with a coercion to an object type.

Note however that it would be probably simpler to turn your functors
into parameterized classes: then you can specify constraints on the
parameters with #-types.

class ['a] r = object
  constraint 'a = #o
  val mutable l : 'a list = []
  method register o = l <- o :: l
  method react = List.iter (fun o -> o#react) l
end

class ['a] r' = object
  constraint 'a = #o'
  inherit ['a] r
  method render = List.iter (fun o -> o#render) l
end

But I don't know what you have precisely in mind.

     Jacques

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners