From: Goswin von Brederlow <goswin-...@web.de>
> Small extra question concerning this. Can I get ocaml to recognise a
> type like this?
> 
> type base = 'a. {
>   x : 'a;
>   fn : 'a -> unit;
> }
> 
> List.iter
>   (fun r -> r.fn r)
>   [{x = 1; fn = (fun r -> print_int r.x); };
>    {x = 1.2; fn = (fun r -> print_float r.x); }]
> 
> The difference to a "'a base" type would be that the 'a is only
> infered and fixed inside the record but remains abstract outside of
> it.

First reaction: but your "base" type is just a closure. But this is
certainly not your question.

More disturbing: the rest of your code does not agree with base.
So I will assume you actually meant

List.iter
  (fun r -> r.fn r.x)
  [{x = 1; fn = print_int}; {x = 1.2; fn = print_float}]

Then what you are asking for is existential types. There is no syntax
for them in ocaml, but they can be encoded through universal types
(interestingly, the dual is not true).

type 'a base = {x : 'a; fn : 'a -> unit}
type 'b base_op = {bop: 'a. 'a base -> 'b}
type base_wrapper = {base: 'b. 'b base_op -> 'b}

let l =
  let a = {x = 1; fn = print_int}
  and b = {x = 1.2; fn = print_float} in
  [{base = fun x -> x.bop a}; {base = fun x -> x.bop b}]

List.iter (fun w -> w.base {bop = fun r -> r.fn r.x}) l

As you can see, the result is rather verbose, but this works.
Fortunately, closure and objects are usually enough...

Jacques Garrigue

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Reply via email to