From: William Lovas <wlovas@stwing.upenn.edu>
To: "Harrison, John R" <johnh@ichips.intel.com>
Cc: caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] Pattern matching but no construction?
Date: Thu, 28 Oct 2004 18:33:46 -0400 [thread overview]
Message-ID: <20041028223346.GB5076@force.stwing.upenn.edu> (raw)
In-Reply-To: <012676D607FCF54E986746512C22CE7D01FF2E0B@orsmsx407>
On Thu, Oct 28, 2004 at 02:34:49PM -0700, Harrison, John R wrote:
> Is there a way to use the OCaml module system to declare an
> abstract type with an implementation as a recursive type in
> such a way that:
>
> * You can use the constructors to pattern-match against
>
> * You cannot use the constructors to construct values
Indeed, there is! The relevant keyword is `private'. See:
http://caml.inria.fr/ocaml/htmlman/manual021.html#@manual.kwd173
for details.
In your example, we just do:
> For example, suppose I do the following:
>
> module type Wibble =
> (* sig type thing = Integer of int | Boolean of bool *)
sig type thing = private Integer of int | Boolean of bool
> val mk_thing : int -> thing
> val dest_thing: thing -> int
> end;;
>
> [...]
And then it works as expected:
# Thing.Integer 5;;
Cannot create values of the private type Thing.thing
# let thing = Thing.mk_thing 5;;
val thing : Thing.thing = Thing.Integer 5
# match thing with
Thing.Integer i -> i
| Thing.Boolean b -> 0;;
- : int = 5
cheers,
William
next prev parent reply other threads:[~2004-10-28 22:33 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-10-28 21:34 Harrison, John R
2004-10-28 22:30 ` [Caml-list] " Jon Harrop
2004-10-28 22:33 ` William Lovas [this message]
2004-10-28 22:36 ` brogoff
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=20041028223346.GB5076@force.stwing.upenn.edu \
--to=wlovas@stwing.upenn.edu \
--cc=caml-list@inria.fr \
--cc=johnh@ichips.intel.com \
/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