It is possible to generalize this approach using private row types. 

list_fns.mli:
module Registry(Obj : sig
    type t = private < .. >
  end) : sig
  val register_obj : string -> Obj.t -> unit
  val get_obj : string -> Obj.t
end

list_fns.ml:
module Registry(Obj : sig
    type t = private < .. >
  end) = struct

  let objs = ref []
  let register_obj name obj = objs := (name, obj) :: !objs
  let get_obj name = List.assoc name !objs
end

test.ml:

class foo = object
  method hello = "hello from foo"
  method goodbye () = print_endline "goodbye"
end

class bar = object
  method hello = "hello from bar"
end

module Bars = List_fns.Registry(struct type t = bar end)

let () =
  let o1 = new foo in
  let o2 = new bar in
  Bars.register_obj "o1" (o1 :> bar);
  Bars.register_obj "o2" (o2 );
  print_endline ("calling o1: " ^ (Bars.get_obj "o1")#hello);
  print_endline ("calling o2: " ^ (Bars.get_obj "o2")#hello)



The List_fns.Registry functor will create a monomorphic registry for the specified base type. 


Probably, this may help you :)

Best wishes,
Ivan Gotovchits

On Wed, Feb 22, 2017 at 12:38 PM, Richard W.M. Jones <rich@annexia.org> wrote:
On Wed, Feb 22, 2017 at 06:28:51PM +0100, Damien Guichard wrote:
>
> Hi Richard.
>
> Is this the code you want ?
>
> type 'a obj_list = 'a list
> constraint 'a = < hello : string; .. > as 'a

Perhaps.  Is it possible to write the list_fns.mli interface
using this?

Rich.


--
Caml-list mailing list.  Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs