caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Martin Jambon <mjambon@gmail.com>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Default value in module
Date: Wed, 13 Sep 2017 11:56:30 -0700	[thread overview]
Message-ID: <8efb84a4-eb53-1e7f-8b7a-406834a3e41d@gmail.com> (raw)
In-Reply-To: <CAMSKV5c0r-nbx6gL=jKTU44YghM3qY+qV0xdTFSW-8HA-TWaYw@mail.gmail.com>

On 09/12/2017 02:29 PM, Paul A. Steckler wrote:
> On Tue, Sep 12, 2017 at 5:21 PM, Yotam Barnoy <yotambarnoy@gmail.com> wrote:
>> The easiest thing to do is to create a prototype module with the value
>> you want, and to include that module (include Foo) in any module you
>> want to have that basic structure.
> 
> Yes, but that means I have to write "include Foo" to get the default
> value. My goal is to avoid writing anything extra when I'm using the
> default value.

I personally find this solution quite reasonable. You'd write this:

module type A = sig
   val x : int
end

module A_defaults = struct
   let x = 42
end

module A1 : A = struct
   include A_defaults
   let x = 17
end

module A2 : A = struct
   include A_defaults
end

which is really nicely written as:

module A2 : A = A_defaults

Now if you have extra module items to add or to override, the cost of 
inserting the line `include A_defaults` becomes quickly negligible, as 
in the `B1` definition below:

module type B = sig
   val x : int
   val y : int
   val z : int
end

module B_defaults = struct
   let x = 42
   let y = 17
end

module B1 : B = struct
   include B_defaults
   let y = 21
   let z = -1
end

Martin

  reply	other threads:[~2017-09-13 18:56 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-12 20:00 Paul A. Steckler
2017-09-12 21:17 ` SP
2017-09-12 21:21 ` Yotam Barnoy
2017-09-12 21:29   ` Paul A. Steckler
2017-09-13 18:56     ` Martin Jambon [this message]
     [not found] ` <21484041-b7ed-33f0-4434-6817a82289fe@lexifi.com>
2017-09-12 21:49   ` Paul A. Steckler
2017-09-13 18:57     ` Martin Jambon

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=8efb84a4-eb53-1e7f-8b7a-406834a3e41d@gmail.com \
    --to=mjambon@gmail.com \
    --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).