Browse thread
Subtyping
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: | 2009-04-08 (01:35) |
From: | Jacques Garrigue <garrigue@m...> |
Subject: | Re: [Caml-list] Subtyping |
From: Goswin von Brederlow <goswin-v-b@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