From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id JAA16939; Fri, 27 Feb 2004 09:40:36 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id JAA17529 for ; Fri, 27 Feb 2004 09:40:35 +0100 (MET) Received: from dell.nogin.org (charter-242-037.caltech.edu [131.215.242.37]) by nez-perce.inria.fr (8.12.10/8.12.10) with ESMTP id i1R8ebIq031535 for ; Fri, 27 Feb 2004 09:40:38 +0100 Received: from cs.caltech.edu (localhost.localdomain [127.0.0.1]) by dell.nogin.org (8.12.11/8.12.9) with ESMTP id i1R8dO1V030778 for ; Fri, 27 Feb 2004 00:39:24 -0800 Message-ID: <403F023B.2000102@cs.caltech.edu> Date: Fri, 27 Feb 2004 00:39:23 -0800 From: Aleksey Nogin Organization: California Institute of Technology, Computer Science Department User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7a) Gecko/20040115 Netscape/7.1 X-Accept-Language: ru, en-us, en MIME-Version: 1.0 To: Caml List Subject: Re: [Caml-list] extensible records? References: <20040227004557.C85CE9BBA2@orchestra.cs.caltech.edu> In-Reply-To: <20040227004557.C85CE9BBA2@orchestra.cs.caltech.edu> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce by Joe's j-chkmail ("http://j-chkmail.ensmp.fr")! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 extensible:01 vanier:01 mli:01 infer:01 mli:01 3.06:01 val:01 foo:01 val:01 foo:01 inferred:01 passing:01 626:99 compiler:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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. Consider % 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 = # let run ( f : [> ] data -> unit) = f (Ext (`Foo 1));; val run : ([> `Foo of int] data -> unit) -> unit = # 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