Mailing list for all users of the OCaml language and system.
 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:57:36 -0700	[thread overview]
Message-ID: <9b22131c-bdfc-3003-23e9-b3a7c06ef34f@gmail.com> (raw)
In-Reply-To: <CAMSKV5cEwqqJzU2kpi18MMvLGvMg8Ei80BjXWtHbXnV=6hYs4g@mail.gmail.com>

On 09/12/2017 02:49 PM, Paul A. Steckler wrote:
> On Tue, Sep 12, 2017 at 5:40 PM, Alain Frisch <alain.frisch@lexifi.com> wrote:
>> What would be allowed as a default value is not completely clear (you don't
>> want to carry runtime values with module types).
> 
> Yes, propagating the default value sounds like the main implementation obstacle.

A possible solution inspired from atdgen is to support only expressions 
that come with no uncertain dependency and no "computation", i.e. 
literal constants of core types. Core types here would include unit, 
bool, int, float, string, and list/option of a core type, and a few more 
(int64 etc.) and would exclude mutable values, records, variants other 
than list/option, and objects.

Then we could write, using atd's conventions for `~` and `?`:

module type A = sig
   val ~x : int = 42 (* just a literal,
                        whose type can be inferred syntactically *)
end

module A1 : A = struct
   let x = 17
end

module A2 : A = struct
   (* implicit `let x = 42` *)
end

Additionally, we could also have canonical defaults:

bool: false
int: 0
float: 0.
string: ""
option: None
list: []

These being common and intuitive defaults, it makes sense to support 
them. So we could write:

module type A = sig
   val ~x : int
end

module A1 : A = struct
   let x = 17
end

module A3 : A = struct
   (* implicit `let x = 0` *)
end

I think this feature would be most useful if it was supported on record 
types.

A record definition equivalent to the module signature above would be:

type a = {
   ~x: int;         (* default value is 0 *)
   ~y: int = 42;    (* default value is 42 *)
   ?z: int option;  (* default value is None *)
}

let a1 : a = {}
   (* equals `{ x = 0; y = 42; z = None }` *)

Martin

      reply	other threads:[~2017-09-13 18:57 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
     [not found] ` <21484041-b7ed-33f0-4434-6817a82289fe@lexifi.com>
2017-09-12 21:49   ` Paul A. Steckler
2017-09-13 18:57     ` Martin Jambon [this message]

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=9b22131c-bdfc-3003-23e9-b3a7c06ef34f@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