Browse thread
Pattern matching but no construction?
-
Harrison, John R
- William Lovas
- Jon Harrop
- brogoff
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Jon Harrop <jon@j...> |
| Subject: | Re: [Caml-list] Pattern matching but no construction? |
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.