caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: Ashish Agarwal <agarwal1975@gmail.com>
Cc: Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] use identity for default function
Date: Fri, 8 Feb 2013 11:47:59 +0900	[thread overview]
Message-ID: <FD3FD2EB-D2CA-432E-8D81-E99B736ED257@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <CAMu2m2Jz=HywCLqDrQAVZx9Lx+P6n7jAsRZRkbgeatUU2CAeow@mail.gmail.com>

On 2013/02/08, at 4:22, Ashish Agarwal <agarwal1975@gmail.com> wrote:

> I'd like to implement
> 
> val foo : ?f:('a -> 'b) -> 'a -> 'b
> 
> where the default for f is identity. Is there now a way to do this with all the new features of OCaml?

This is an old request (about as old as optional arguments).
The problem is clear enough: you want a different type when no argument is passed.
Interestingly, it would be possible to do that using GADTs:

type (_,_) optional =
  | Override : 'a -> ('a,'b) optional
  | Default : ('a,'a) optional

let foo : type a b. (a -> b, a -> a) optional -> a -> b = function
  | Override f -> f
  | Default -> fun x -> x

Unfortunately, GADTs were not there when optional arguments were introduced.
Also, there is the problem that the type gets more complicated.
(But there could be some sugar to print identical types only once)

Jacques Garrigue

      parent reply	other threads:[~2013-02-08  2:47 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-02-07 19:22 Ashish Agarwal
2013-02-07 19:53 ` Jeff Meister
2013-02-07 20:04   ` AW: " Gerd Stolpmann
2013-02-07 20:06   ` Gabriel Kerneis
2013-02-09 13:13   ` Goswin von Brederlow
2013-02-09 20:10     ` Jeff Meister
2013-02-07 19:59 ` Martin Jambon
2013-02-07 20:12   ` Gabriel Kerneis
2013-02-07 20:57   ` Ashish Agarwal
2013-02-08  2:47 ` Jacques Garrigue [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=FD3FD2EB-D2CA-432E-8D81-E99B736ED257@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=agarwal1975@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).