From: Aleksey Nogin <nogin@cs.caltech.edu>
To: Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] extensible records?
Date: Fri, 27 Feb 2004 00:39:23 -0800 [thread overview]
Message-ID: <403F023B.2000102@cs.caltech.edu> (raw)
In-Reply-To: <20040227004557.C85CE9BBA2@orchestra.cs.caltech.edu>
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 = <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
next prev parent reply other threads:[~2004-02-27 8:40 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-02-27 0:45 Michael Vanier
2004-02-27 1:04 ` Kenneth Knowles
2004-02-27 8:39 ` Aleksey Nogin [this message]
2004-02-27 8:39 ` Nicolas Cannasse
2004-02-27 11:17 ` Alexander S. Usov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=403F023B.2000102@cs.caltech.edu \
--to=nogin@cs.caltech.edu \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox