caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
To: rich@annexia.org
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Fwd: Polymorphic optional label argument, with default
Date: Sun, 11 Apr 2004 22:16:19 +0900	[thread overview]
Message-ID: <20040411221619G.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: <20040411083357.GA19305@redhat.com>

From: Richard Jones <rich@annexia.org>
> On Sun, Apr 11, 2004 at 01:26:24AM -0500, Brian Hurt wrote:
> > >   plot : ?labels : ('a -> string) -> (* ... 'a ... *) -> unit
> > > 
> > > [The 'a types are the same type.  If I leave out the initializer, then
> > > it works.]
> > 
> > I don't think it's possible.  Consider the following situation: I pass in 
> > a graph of floats for example, and then forget to specify a labels 
> > argument.  Now you're passing a float to string_of_int.
> 
> I'd want this to generate a compile-time error, because the 'a 's
> aren't the same.
> 
> It's perfectly possible to define this function if one leaves out the
> default argument, or sets the default argument to a function typed as
> 'a -> string.
> 
> # let plot ~labels graph = print_endline (labels graph);;
> val plot : labels:('a -> string) -> 'a -> unit = <fun>
> 
> or:
> 
> # let string_of_any x = "foo";;
> val string_of_any : 'a -> string = <fun>
> # let plot ?(labels = string_of_any) graph = print_endline (labels graph);;
> val plot : ?labels:('a -> string) -> 'a -> unit = <fun>
> 
> But not if I want the default to be the most common case (which is
> that my graph will be a graph of ints).  I want the common case in
> there so that most of the time end users of the API won't need to
> worry about ~labels and optional arguments.

What you are asking for has already been discussed on this list, a few
years ago. Here is my answer at that time:
  http://caml.inria.fr/archives/200102/msg00212.html

Basically, what you are asking for is a new type of constraint, which
is only to be applied when the optional argument is omitted (and as
result the default is selected).
This is not possible in the current type system, and while there are
some uses for that, it would be hard to justify making types more
complex in the general case, just to handle this specific problem.

Note that there is an encoding, which does not provide optional
arguments, but just a way to handle this kind of default in an
explicit way.

module Opt : sig
  type ('a,'b) t
  val omit : ('a,'a) t
  val arg : 'a -> ('a,'b) t
  val get : default:'b -> ('a,'b) t -> 'a
end = struct
  type ('a,'b) opt = Omit | Arg of 'a
  let omit = Omit
  let arg x = Arg x
  let get ~default = function
      Omit -> Obj.magic default
    | Arg x -> x
end

Note that the typing makes this use of Obj.magic OK.

Then your function may be defined to take as argument either Opt.omit
or (Opt.arg f).

This probably doesn't solve your problem, but this may help you to see
the issues involved.

Jacques Garrigue

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  parent reply	other threads:[~2004-04-11 13:16 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-04-10 21:36 Richard Jones
2004-04-10 22:40 ` Gerd Stolpmann
2004-04-11  0:03   ` skaller
2004-04-10 23:52 ` Matt Gushee
2004-04-11  6:26 ` Brian Hurt
2004-04-11  8:33   ` Richard Jones
2004-04-11  9:01     ` skaller
2004-04-11 13:16     ` Jacques Garrigue [this message]
2004-04-11 15:23       ` nadji
2004-04-12  2:25         ` Jacques Garrigue

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=20040411221619G.garrigue@kurims.kyoto-u.ac.jp \
    --to=garrigue@kurims.kyoto-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=rich@annexia.org \
    /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).