English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] extensible records?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-02-27 (08:40)
From: Aleksey Nogin <nogin@c...>
Subject: Re: [Caml-list] extensible records?
On 26.02.2004 16:45, Michael Vanier wrote:

> I thought of using polymorphic variant types, but my (limited) understanding
> is that you either declare all possible variants straight up in the .mli file
> (which totally defeats my purpose) or you don't declare anything and let the
> compiler infer the type (which I don't know how to write into a .mli file).

I have to admit that I have never used polymorphic variant types myself, 
but the way I understand them, they should probably work for you. If I 
understand it correctly ou do not have to declare them _exactly_ in the 
.mli file - e.g. you could use [> ] in the .mli file and something more 
specific in the .ml file.


% ocaml

         Objective Caml version 3.06

# type 'a data = Int of int | Ext of 'a;;
type 'a data = Int of int | Ext of 'a
# let open_file f =
      match f with
         Ext `File f -> print_string ("Opening file" ^ f)
       | _ -> raise (Invalid_argument "open_file")
val open_file : [> `File of string] data -> unit = <fun>
# let run ( f :  [> ] data -> unit) = f (Ext (`Foo 1));;
val run : ([> `Foo of int] data -> unit) -> unit = <fun>
# run open_file;;
Exception: Invalid_argument "open_file".

Note that open_file does have the type
   [> ] data -> unit
(in addition to having the inferred type
   [> `File of string] data -> unit
) - passing it to run is not a type error.

So with something like the data type definition above you can use  [> ] 
data as an input type in your .mli and use a more specific type as an 
output type in your .mli

P.S. In your examples you use nested "match" statements instead nested 
patterns. It's ugly to read (IMHO) and less efficient (AFAIK).

Aleksey Nogin

Home Page: http://nogin.org/
E-Mail: nogin@cs.caltech.edu (office), aleksey@nogin.org (personal)
Office: Jorgensen 70, tel: (626) 395-2907

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