caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Labels at the module level?
@ 2015-06-23  8:26 Kenichi Asai
  2015-06-23  8:43 ` Jeremy Yallop
  2015-06-24  0:25 ` Kenichi Asai
  0 siblings, 2 replies; 6+ messages in thread
From: Kenichi Asai @ 2015-06-23  8:26 UTC (permalink / raw)
  To: caml-list

Using labeled arguments, one can supply default values for unspecified
arguments:

let f ?(x = 3) y = x * y
in f 5

will return 15 without specifying the value of x at the second line.
Is there a way to do a similar thing at the module level?  Namely, 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.

Background: in the universe library for OCaml:

http://pllab.is.ocha.ac.jp/~asai/Universe/

one specifies various handlers for events (such as tick, mouse, and
key events).  Currently, these handlers are registered to the big_bang
function that accepts them using labeled arguments (with default
values).  I wonder if I can specify the interface as a module
signature and let the user write a module of that signature but only
those handlers that the user is interested in.

-- 
Kenichi Asai

^ permalink raw reply	[flat|nested] 6+ messages in thread
* [Caml-list]  Labels at the module level?
@ 2015-06-23  8:50 Oleg
  0 siblings, 0 replies; 6+ messages in thread
From: Oleg @ 2015-06-23  8:50 UTC (permalink / raw)
  To: caml-list; +Cc: asai


> 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.


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2015-06-26  8:12 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-23  8:26 [Caml-list] Labels at the module level? 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
2015-06-23  8:50 Oleg

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).