From: Alain Frisch <frisch@clipper.ens.fr>
To: Caml list <caml-list@inria.fr>
Subject: Optional fields in modules
Date: Thu, 4 Jan 2001 19:05:52 +0100 (MET) [thread overview]
Message-ID: <Pine.GSO.4.04.10101041859160.7576-100000@clipper.ens.fr> (raw)
Hello,
I propose a tiny extension to the OCaml module system to allow optional
specification of *values* in signatures; for instance (I re-used the
keyword virtual):
sig virtual val f : int -> int end
A structure matches this signature if:
- it defines a value (or external primitive) f with the correct type,
or
- it doesn't define the value f
When accessing a field of a structure with this signature,
f has type (int -> int) option.
So, the coercion to this module type lifts the present identifiers
(convert them to "Some ...") and put "None" where absent identifiers are
expected.
This is very similar to optional arguments.
A typical use is to define "enriching functors": when some fields of the
argument are absent, replace them with default values.
Here is an example:
------------
module type WRITER = sig
type t
val string : t -> string -> unit
val char : t -> char -> unit
end;;
module type WRITER_SKEL = sig
type t
virtual val string : t -> string -> unit
virtual val char : t -> char -> unit
end;;
module FILL_IN (W : WRITER_SKEL) : WRITER with type t = W.t = struct
type t = W.t
let string, char =
match W.string, W.char with
| Some string, Some char -> string, char
| Some string, None ->
string,
(fun w c -> string w (String.make 1 c))
| None, Some char ->
(fun w s -> for i = 0 to String.length s - 1 do char w s.[i] done),
char
| None, None -> failwith "Provide at least one of string, char !"
end;;
module Test = struct
type t = unit
(* you can comment out one of : *)
let string () s = Printf.printf "String [%s]\n" s
let char () c = Printf.printf "Char [%c]\n" c
end;;
module Test' = FILL_IN (Test);;
------------
The system is not powerful enough to express constraints such as
"string or char may be absent, but not both" (it is easy
to check this statically, but I don't see any light syntax to express the
constraint).
I wrote a patch (against OCaml 3.00) implementing this proposal:
http://www.eleves.ens.fr:8080/home/frisch/info/patch-option
Supposing you are in a fresh source tree, you can apply it
with "patch -Np1 < patch-option", then build the system as usual.
Comments are welcome.
--
Alain Frisch
reply other threads:[~2001-01-04 20:19 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=Pine.GSO.4.04.10101041859160.7576-100000@clipper.ens.fr \
--to=frisch@clipper.ens.fr \
--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;
as well as URLs for NNTP newsgroup(s).