From: Jon Harrop <jon@jdh30.plus.com>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Pattern matching but no construction?
Date: Thu, 28 Oct 2004 23:30:01 +0100 [thread overview]
Message-ID: <200410282330.01081.jon@jdh30.plus.com> (raw)
In-Reply-To: <012676D607FCF54E986746512C22CE7D01FF2E0B@orsmsx407>
You can hide the types held by the Integer and Boolean constructors and not
hide the variant type itself:
# module Thing : sig
type integer
type boolean
type t = Integer of integer | Boolean of boolean
val mk_thing : int -> t
val dest_thing : t -> int
end = struct
type integer = int
type boolean = bool
type t = Integer of integer | Boolean of boolean
let mk_thing i = Integer i
let dest_thing t = match t with
Integer i -> i
| Boolean b -> if b then 1 else 0
end;;
module Thing :
sig
type integer
type boolean
type t = Integer of integer | Boolean of boolean
val mk_thing : int -> t
val dest_thing : t -> int
end
Then you could "open" the namespace of the Thing module, saving enormously on
typing:
# open Thing;;
Then you can use pattern matching to determine if a value of type "Thing.t"
uses the "Integer" or the "Boolean" constructor:
# fun (Boolean b) -> b;;
Warning: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
Integer _
- : Thing.t -> Thing.boolean = <fun>
A better example might be:
# let is_bool = function Integer _ -> false | Boolean _ -> true;;
val is_bool : Thing.t -> bool = <fun>
Of course, if your pattern catches a value of type "Thing.integer" or
"Thing.boolean" then you can't do anything with it except hand it to a
function in the "Thing" module.
But you can't actually construct a "Thing.t" because you don't have access to
the hidden "integer" and "boolean" types in "Thing":
# Integer(3);;
This expression has type int but is here used with type Thing.integer
As an aside which you may well already know, convention is to use a type "t"
for the main type of a module (e.g. List.t, Array.t, String.t) and a function
"make" to construct it. Possibly also a function "compare" so you can build
sets and maps over the type, e.g.:
# module StringSet = Set.Make(String);;
...
Cheers,
Jon.
next prev parent reply other threads:[~2004-10-28 22:34 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 ` Jon Harrop [this message]
2004-10-28 22:33 ` [Caml-list] " William Lovas
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=200410282330.01081.jon@jdh30.plus.com \
--to=jon@jdh30.plus.com \
--cc=caml-list@yquem.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