caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Oleg <oleg@okmij.org>
To: caml-list@inria.fr
Cc: asai@is.ocha.ac.jp
Subject: [Caml-list]  Labels at the module level?
Date: Tue, 23 Jun 2015 17:50:11 +0900	[thread overview]
Message-ID: <20150623085011.GA1283@Magus.sf-private> (raw)


> I want to define a functor that accepts a module of sig:
> module type A_t = sig
>   val x : int
>   val y : int
> end
> but if the user did not specify some of the values in the signature,
> some default values will be used.

It is even easier at the module level: include comes in handy. Suppose
we have a consumer

module Universe(Params: A_t) = struct
  let r () = Printf.printf "x = %d y = %d\n" Params.x Params.y
end;;

First, we define defaults:

module Def : A_t = struct let x = 1 let y = 2 end;;

We can invoke it as

let module M = Universe(Def) in M.r ();;

and also as

let module M = Universe(struct include Def let x = 100 end) in M.r ();;

BTW, groups of related parameters could be further grouped into
submodules.


             reply	other threads:[~2015-06-23  8:42 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-23  8:50 Oleg [this message]
  -- strict thread matches above, loose matches on Subject: below --
2015-06-23  8:26 Kenichi Asai
2015-06-23  8:43 ` Jeremy Yallop
2015-06-23  8:46   ` Benjamin Greenman
2015-06-24  0:25 ` Kenichi Asai
2015-06-26  8:12   ` Ben Millwood

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=20150623085011.GA1283@Magus.sf-private \
    --to=oleg@okmij.org \
    --cc=asai@is.ocha.ac.jp \
    --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).